-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplugin.cpp
191 lines (156 loc) · 4.06 KB
/
plugin.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
#include "stdafx.h"
#include "plugin.h"
#include "linter.h"
#include "notepad/Scintilla.h"
#include "XmlParser.h"
#include <string>
const TCHAR PLUGIN_NAME[] = L"Linter";
TCHAR iniFilePath[MAX_PATH];
static const int FUNCTIONS_COUNT = 1;
FuncItem funcItem[FUNCTIONS_COUNT];
NppData nppData;
HANDLE timers(0);
BOOL APIENTRY DllMain(HANDLE hModule, DWORD reasonForCall, LPVOID /*lpReserved*/)
{
switch (reasonForCall)
{
case DLL_PROCESS_ATTACH:
pluginInit(hModule);
break;
case DLL_PROCESS_DETACH:
pluginCleanUp();
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
extern "C" __declspec(dllexport) void setInfo(NppData notpadPlusData)
{
nppData = notpadPlusData;
commandMenuInit();
initConfig();
}
extern "C" __declspec(dllexport) const TCHAR *getName()
{
return PLUGIN_NAME;
}
extern "C" __declspec(dllexport) FuncItem *getFuncsArray(int *nbF)
{
*nbF = FUNCTIONS_COUNT;
return funcItem;
}
extern "C" __declspec(dllexport) LRESULT messageProc(UINT /*Message*/, WPARAM /*wParam*/, LPARAM /*lParam*/)
{
return TRUE;
}
extern "C" __declspec(dllexport) BOOL isUnicode()
{
return TRUE;
}
void pluginInit(HANDLE /*hModule*/)
{
timers = CreateTimerQueue();
}
void pluginCleanUp()
{
}
TCHAR *getIniFileName()
{
return iniFilePath;
}
void initConfig()
{
::SendMessage(nppData._nppHandle, NPPM_GETPLUGINSCONFIGDIR, MAX_PATH, (LPARAM)iniFilePath);
if (PathFileExists(iniFilePath) == FALSE)
{
::CreateDirectory(iniFilePath, NULL);
}
PathAppend(iniFilePath, L"linter.xml");
}
void editConfig()
{
::SendMessage(nppData._nppHandle, NPPM_DOOPEN, 0, (LPARAM)iniFilePath);
}
bool setCommand(size_t index, TCHAR *cmdName, PFUNCPLUGINCMD pFunc, ShortcutKey *sk, bool check0nInit)
{
if (index >= FUNCTIONS_COUNT)
{
return false;
}
if (!pFunc)
{
return false;
}
lstrcpy(funcItem[index]._itemName, cmdName);
funcItem[index]._pFunc = pFunc;
funcItem[index]._init2Check = check0nInit;
funcItem[index]._pShKey = sk;
return true;
}
void commandMenuInit()
{
setCommand(0, bstr_t(L"Edit config"), editConfig, NULL, false);
}
void commandMenuCleanUp()
{
}
HWND getScintillaWindow()
{
int which = -1;
SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
if (which == -1)
{
return NULL;
}
return (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
}
LRESULT SendEditor(UINT Msg, WPARAM wParam, LPARAM lParam)
{
HWND wEditor = getScintillaWindow();
return SendMessage(wEditor, Msg, wParam, lParam);
}
LRESULT SendApp(UINT Msg, WPARAM wParam, LPARAM lParam)
{
return SendMessage(nppData._nppHandle, Msg, wParam, lParam);
}
std::string getDocumentText()
{
LRESULT lengthDoc = SendEditor(SCI_GETLENGTH) + 1;
char *buff = new char[lengthDoc];
SendEditor(SCI_GETTEXT, lengthDoc, (LPARAM)buff);
std::string text(buff, lengthDoc);
text = text.c_str();
delete[] buff;
return text;
}
std::string getLineText(int line)
{
LRESULT length = SendEditor(SCI_LINELENGTH, line);
char *buff = new char[length + 1];
SendEditor(SCI_GETLINE, line, (LPARAM)buff);
std::string text(buff, length);
text = text.c_str();
delete[] buff;
return text;
}
LRESULT getPositionForLine(int line)
{
return SendEditor(SCI_POSITIONFROMLINE, line);
}
void ShowError(LRESULT start, LRESULT end, bool off)
{
LRESULT oldid = SendEditor(SCI_GETINDICATORCURRENT);
SendEditor(SCI_SETINDICATORCURRENT, SCE_SQUIGGLE_UNDERLINE_RED);
if (off)
{
SendEditor(SCI_INDICATORFILLRANGE, start, (end - start));
}
else
{
SendEditor(SCI_INDICATORCLEARRANGE, start, (end - start));
}
SendEditor(SCI_SETINDICATORCURRENT, oldid);
}