-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathNppSnippets.cpp
441 lines (366 loc) · 11.9 KB
/
NppSnippets.cpp
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/////////////////////////////////////////////////////////////////////////////
// //
// NppSnippets - Code Snippets plugin for Notepad++ //
// Copyright (C) 2010-2020 Frank Fesevur //
// //
// 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. //
// //
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <assert.h>
#include <commctrl.h>
#include "NPP/PluginInterface.h"
#include "Resource.h"
#include "NppSnippets.h"
#include "DlgAbout.h"
#include "DlgConsole.h"
#include "DlgOptions.h"
#include "Options.h"
#include "SnippetsDB.h"
#ifdef _MSC_VER
#pragma comment(lib, "comctl32.lib")
#endif
static const TCHAR PLUGIN_NAME[] = L"Snippets";
static const int nbFunc = 11;
static HBITMAP hbmpToolbar = NULL;
HINSTANCE g_hInst;
NppData g_nppData;
LangType g_currentLang = L_TEXT; // Current language of the N++ text window
FuncItem g_funcItem[nbFunc];
Options *g_Options = NULL;
bool g_HasLangMsgs = false;
/////////////////////////////////////////////////////////////////////////////
//
extern "C" __declspec(dllexport) void setInfo(NppData notpadPlusData)
{
g_nppData = notpadPlusData;
}
/////////////////////////////////////////////////////////////////////////////
//
extern "C" __declspec(dllexport) const TCHAR* getName()
{
return PLUGIN_NAME;
}
/////////////////////////////////////////////////////////////////////////////
//
extern "C" __declspec(dllexport) FuncItem* getFuncsArray(int *nbF)
{
*nbF = nbFunc;
return g_funcItem;
}
/////////////////////////////////////////////////////////////////////////////
//
HWND getCurrentScintilla()
{
int current = -1;
SendMessage(g_nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤t);
return (current == 0) ? g_nppData._scintillaMainHandle : g_nppData._scintillaSecondHandle;
}
/////////////////////////////////////////////////////////////////////////////
//
extern "C" __declspec(dllexport) BOOL isUnicode()
{
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
//
extern "C" __declspec(dllexport) void beNotified(SCNotification* notifyCode)
{
switch (notifyCode->nmhdr.code)
{
case NPPN_READY:
{
// Initialize the database
g_db = new SnippetsDB();
if (g_Options->GetShowConsoleDlg())
SnippetsConsole();
break;
}
case NPPN_SHUTDOWN:
{
break;
}
case NPPN_TBMODIFICATION:
{
// First initialize the options
g_Options = new Options(L"NppSnippets.ini");
// Do we need to load the toolbar icon?
if (g_Options->GetToolbarIcon())
{
// Add the button to the toolbar
toolbarIconsWithDarkMode tbiFolder;
hbmpToolbar = CreateMappedBitmap(g_hInst, IDB_SNIPPETS, 0, 0, 0);
tbiFolder.hToolbarBmp = hbmpToolbar;
tbiFolder.hToolbarIcon = NULL;
tbiFolder.hToolbarIconDarkMode = NULL;
SendMessage((HWND) notifyCode->nmhdr.hwndFrom, NPPM_ADDTOOLBARICON_FORDARKMODE, (WPARAM) g_funcItem[0]._cmdID, (LPARAM) &tbiFolder);
}
break;
}
case NPPN_LANGCHANGED:
case NPPN_BUFFERACTIVATED:
{
LangType lang;
SendMessage(g_nppData._nppHandle, NPPM_GETCURRENTLANGTYPE, 0, (LPARAM) &lang);
if (g_currentLang != lang)
{
g_currentLang = lang;
UpdateSnippetsList();
}
break;
}
case NPPN_WORDSTYLESUPDATED:
{
InvalidateListbox();
break;
}
}
}
/////////////////////////////////////////////////////////////////////////////
// Here you can process the Npp Messages
// I will make the messages accessible little by little, according to the
// need of plugin development.
// Please let me know if you need to access to some messages :
// http://sourceforge.net/forum/forum.php?forum_id=482781
extern "C" __declspec(dllexport) LRESULT messageProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(uMsg);
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
/*
if (uMsg == WM_MOVE)
{
MsgBox("move");
}
*/
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// VERY simple LF -> CRLF conversion
std::wstring ConvertLineEnding(LPCWSTR from, int toLineEnding)
{
// Is there a string anyway?
std::wstring to;
if (from == NULL)
return to;
// Get the line ending of the current document
if (toLineEnding < 0)
toLineEnding = (int) SendMsg(SCI_GETEOLMODE);
// Determine the new line ending
std::wstring lineend = L"\r\n";
switch (toLineEnding)
{
case SC_EOL_CRLF:
lineend = L"\r\n";
break;
case SC_EOL_CR:
lineend = L"\r";
break;
case SC_EOL_LF:
lineend = L"\n";
break;
}
// Iterate through the text we were given
size_t len = wcslen(from);
for (size_t i = 0; i < len; i++)
{
if (from[i] == '\r')
{
if (i + 1 < len)
{
// is the next a \n, skip it
if (from[i + 1] == '\n')
i++;
// Add the new line ending
to += lineend;
continue;
}
}
if (from[i] == '\n')
{
to += lineend;
continue;
}
to += from[i];
}
return to;
}
/////////////////////////////////////////////////////////////////////////////
// Easy access to the MessageBox functions
void MsgBox(const WCHAR* msg)
{
MessageBox(g_nppData._nppHandle, msg, PLUGIN_NAME, MB_OK);
}
void MsgBox(const char* msg)
{
std::wstring wsTmp(msg, msg + strlen(msg));
MessageBox(g_nppData._nppHandle, wsTmp.c_str(), PLUGIN_NAME, MB_OK);
}
bool MsgBoxYesNo(const WCHAR* msg)
{
return (MessageBox(g_nppData._nppHandle, msg, PLUGIN_NAME, MB_YESNO) == IDYES);
}
/////////////////////////////////////////////////////////////////////////////
// Send a simple message to the Notepad++ window 'count' times and return
// the last result.
LRESULT SendMsg(UINT Msg, WPARAM wParam, LPARAM lParam, int count)
{
LRESULT res = 0;
for (int i = 0; i < count; i++)
res = SendMessage(getCurrentScintilla(), Msg, wParam, lParam);
return res;
}
/////////////////////////////////////////////////////////////////////////////
// Make the window center, relative the NPP-window
void CenterWindow(HWND hDlg)
{
RECT rc;
GetClientRect(g_nppData._nppHandle, &rc);
POINT center;
center.x = rc.left + ((rc.right - rc.left) / 2);
center.y = rc.top + ((rc.bottom - rc.top) / 2);
ClientToScreen(g_nppData._nppHandle, ¢er);
RECT dlgRect;
GetClientRect(hDlg, &dlgRect);
const int x = center.x - ((dlgRect.right - dlgRect.left) / 2);
const int y = center.y - ((dlgRect.bottom - dlgRect.top) / 2);
SetWindowPos(hDlg, HWND_TOP, x, y, -1, -1, SWP_NOSIZE | SWP_SHOWWINDOW);
}
/////////////////////////////////////////////////////////////////////////////
//
int GetDlgTextLength(HWND hDlg, UINT uID)
{
return GetWindowTextLength(GetDlgItem(hDlg, uID));
}
/////////////////////////////////////////////////////////////////////////////
//
std::wstring GetDlgText(HWND hDlg, UINT uID)
{
// Allocate the (temporary) memory needed to store the text
int maxBufferSize = GetDlgTextLength(hDlg, uID) + 3;
WCHAR* buffer = new WCHAR[maxBufferSize];
ZeroMemory(buffer, maxBufferSize);
// Get the text from the dialog item
GetDlgItemText(hDlg, uID, buffer, maxBufferSize);
std::wstring ret = buffer;
delete buffer;
return ret;
}
/////////////////////////////////////////////////////////////////////////////
// Open the on-line manual
static void OpenOnlineManual()
{
ShellExecute(NULL, L"open", L"https://nppsnippets.readthedocs.io", NULL, NULL, SW_SHOWNORMAL);
}
/////////////////////////////////////////////////////////////////////////////
// The entry point of the DLL
BOOL APIENTRY DllMain(HANDLE hModule, DWORD reasonForCall, LPVOID lpReserved)
{
UNREFERENCED_PARAMETER(lpReserved);
switch (reasonForCall)
{
case DLL_PROCESS_ATTACH:
{
g_hInst = (HINSTANCE) hModule;
// The most important menu entry
int index = 0;
g_funcItem[index]._pFunc = SnippetsConsole;
wcscpy(g_funcItem[index]._itemName, L"Snippets");
g_funcItem[index]._init2Check = false;
g_funcItem[index]._pShKey = NULL;
index++;
// Seperator
g_funcItem[index]._pFunc = NULL;
wcscpy(g_funcItem[index]._itemName, L"-SEPARATOR-");
g_funcItem[index]._init2Check = false;
g_funcItem[index]._pShKey = NULL;
index++;
// Focus the items in the snippets panel
g_funcItem[index]._pFunc = FocusLibraryCombo;
wcscpy(g_funcItem[index]._itemName, L"Focus List of Libraries");
g_funcItem[index]._init2Check = false;
g_funcItem[index]._pShKey = NULL;
index++;
g_funcItem[index]._pFunc = FocusFilterSnippets;
wcscpy(g_funcItem[index]._itemName, L"Focus Filter Snippets");
g_funcItem[index]._init2Check = false;
g_funcItem[index]._pShKey = NULL;
index++;
g_funcItem[index]._pFunc = FocusSnippetsList;
wcscpy(g_funcItem[index]._itemName, L"Focus List of Snippets");
g_funcItem[index]._init2Check = false;
g_funcItem[index]._pShKey = NULL;
index++;
// Seperator
g_funcItem[index]._pFunc = NULL;
wcscpy(g_funcItem[index]._itemName, L"-SEPARATOR-");
g_funcItem[index]._init2Check = false;
g_funcItem[index]._pShKey = NULL;
index++;
// Open Options dialog
g_funcItem[index]._pFunc = ShowOptionsDlg;
wcscpy(g_funcItem[index]._itemName, L"Options...");
g_funcItem[index]._init2Check = false;
g_funcItem[index]._pShKey = NULL;
index++;
// Seperator
g_funcItem[index]._pFunc = NULL;
wcscpy(g_funcItem[index]._itemName, L"-SEPARATOR-");
g_funcItem[index]._init2Check = false;
g_funcItem[index]._pShKey = NULL;
index++;
// Open Online Manual
g_funcItem[index]._pFunc = OpenOnlineManual;
wcscpy(g_funcItem[index]._itemName, L"Open Online Manual");
g_funcItem[index]._init2Check = false;
g_funcItem[index]._pShKey = NULL;
index++;
// Seperator
g_funcItem[index]._pFunc = NULL;
wcscpy(g_funcItem[index]._itemName, L"-SEPARATOR-");
g_funcItem[index]._init2Check = false;
g_funcItem[index]._pShKey = NULL;
index++;
// Show About Dialog
g_funcItem[index]._pFunc = ShowAboutDlg;
wcscpy(g_funcItem[index]._itemName, L"About...");
g_funcItem[index]._init2Check = false;
g_funcItem[index]._pShKey = NULL;
index++;
assert(index == nbFunc);
// Create the console dialog
CreateConsoleDlg();
}
break;
case DLL_PROCESS_DETACH:
{
// Don't forget to deallocate your shortcut here
delete g_funcItem[0]._pShKey;
// Delete the toolbar bitmap
if (hbmpToolbar != NULL)
DeleteObject(hbmpToolbar);
// Clean up the options
delete g_Options;
// Clean up the database
delete g_db;
}
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}