-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathIBFPlugin.cpp
350 lines (316 loc) · 12 KB
/
IBFPlugin.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
/////////////////////////////////////////////////////////////////////////////
// //
// IndentByFold - Auto indent based on the fold level //
// Copyright (C) 2011 Ben Bluemel <ben1982@gmail.com> //
// Copyright (C) 2013-2019 Frank Fesevur <fesevur@gmail.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. //
// //
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include "IBFPlugin.h"
#include "IBFMenu.h"
#include "SciMessager.h"
#include "WaitCursor.h"
#include "version_git.h"
extern IBFPlugin ibfplugin;
WNDPROC IBFPlugin::nppOriginalWndProc = NULL;
IBFPlugin::IBFPlugin() {}
IBFPlugin::~IBFPlugin() {}
FuncItem* IBFPlugin::nppGetFuncsArray( int* pnbFuncItems )
{
*pnbFuncItems = IBFMenu::N_NBFUNCITEMS;
return IBFMenu::arrFuncItems;
}
bool IBFPlugin::isNppWndUnicode = true;
LRESULT IBFPlugin::nppCallWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
return ::CallWindowProcW( nppOriginalWndProc, hWnd, uMsg, wParam, lParam );
}
LRESULT CALLBACK IBFPlugin::nppNewWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
return nppCallWndProc( hWnd, uMsg, wParam, lParam );
}
const TCHAR* IBFPlugin::nppGetName()
{
//return PLUGIN_NAME;
return 0;
}
void IBFPlugin::OnLangChanged( uptr_t idFrom )
{
UNREFERENCED_PARAMETER(idFrom);
m_nppMsgr.SendNppMsg( NPPM_GETCURRENTLANGTYPE, (WPARAM) 0, (LPARAM) & langType );
useNextLine = (langType == L_RUBY || langType == L_HTML || langType == L_LISP || langType == L_LUA || langType == L_PASCAL || langType == L_XML);
}
void IBFPlugin::nppBeNotified( SCNotification* notifyCode )
{
if ( notifyCode->nmhdr.hwndFrom == m_nppMsgr.getNppWnd() ) {
// >>> notifications from Notepad++
switch ( notifyCode->nmhdr.code ) {
case NPPN_BUFFERACTIVATED:
case NPPN_FILESAVED:
case NPPN_LANGCHANGED:
OnLangChanged( notifyCode->nmhdr.idFrom );
break;
case NPPN_READY:
OnNppReady();
break;
case NPPN_SHUTDOWN:
OnNppShutdown();
break;
default:
break;
}
// <<< notifications from Notepad++
} else {
bool autocselection = false;
// >>> notifications from Scintilla
switch ( notifyCode->nmhdr.code ) {
case SCN_UPDATEUI:
break;
case SCN_CHARADDED:
OnSciCharAdded( notifyCode->ch );
break;
case SCN_AUTOCSELECTION:
autocselection = true;
case SCN_PAINTED:
case SCN_AUTOCCANCELLED:
if ( ( notifyCode->nmhdr.code != SCN_PAINTED && decrementAfterAutoC ) || (notifyCode->nmhdr.code == SCN_PAINTED && decrementAfterPaint ) ) {
if ( decrementAfterPaint ) decrementAfterPaint = false;
else decrementAfterAutoC = false;
CSciMessager sciMsgr( m_nppMsgr.getCurrentScintillaWnd() );
int curpos = sciMsgr.getCurrentPos();
int curline = sciMsgr.getLineFromPos(curpos);
int curFold = sciMsgr.getFoldLevel( curline + 1 );
if ( postoblame == curpos && ( curFold == foldleveltomatch || notifyCode->nmhdr.code == SCN_PAINTED ) ) {
// Only interested in this if we are completing on the actual text
if ( autocselection && postoblame == notifyCode->position + (int) strlen( notifyCode->text ) ) {
sciMsgr.SendSciMsg( SCI_AUTOCCANCEL );
sciMsgr.setLineIndentation( curline, indentationtouse );
lastFoldDownLine = curline;
} else if ( !autocselection ) {
sciMsgr.setLineIndentation( curline, indentationtouse );
lastFoldDownLine = curline;
}
}
}
break;
case SCN_MODIFIED:
if ( notifyCode->modificationType & SC_MOD_CHANGEFOLD ) {
int foldlevelNowMask = notifyCode->foldLevelNow;
bool foldlevelnowishead = false;
if ( foldlevelNowMask & SC_FOLDLEVELWHITEFLAG ) {
foldlevelNowMask = foldlevelNowMask &~SC_FOLDLEVELWHITEFLAG;
}
if ( foldlevelNowMask & SC_FOLDLEVELHEADERFLAG ) {
foldlevelnowishead = true;
foldlevelNowMask = foldlevelNowMask &~SC_FOLDLEVELHEADERFLAG;
}
int foldlevelNowMask2 = foldlevelNowMask & SC_FOLDLEVELNUMBERMASK ;
int foldlevelPrevMask = notifyCode->foldLevelPrev;
if ( foldlevelPrevMask & SC_FOLDLEVELWHITEFLAG ) {
foldlevelPrevMask = foldlevelPrevMask &~SC_FOLDLEVELWHITEFLAG;
}
if ( foldlevelPrevMask & SC_FOLDLEVELHEADERFLAG ) {
foldlevelPrevMask = foldlevelPrevMask &~SC_FOLDLEVELHEADERFLAG;
}
int foldlevelPrevMask2 = foldlevelPrevMask & SC_FOLDLEVELNUMBERMASK;
bool shifted = false;
if (foldlevelNowMask2 == foldlevelPrevMask2 && foldlevelNowMask != foldlevelPrevMask) {
shifted = true;
foldlevelNowMask = ( notifyCode->foldLevelNow >> 16);
foldlevelPrevMask = ( notifyCode->foldLevelPrev >> 16);
} else {
foldlevelNowMask = foldlevelNowMask2;
foldlevelPrevMask = foldlevelPrevMask2;
}
if ( foldlevelNowMask < foldlevelPrevMask ) {
CSciMessager sciMsgr( m_nppMsgr.getCurrentScintillaWnd() );
int curline = sciMsgr.getLineFromPos(sciMsgr.getCurrentPos());
if ( curline == notifyCode->line || ( useNextLine && curline == notifyCode->line - 1 ) ) {
int actualline = notifyCode->line;
if ( curline == notifyCode->line - 1) {
actualline--;
}
int foldparentline = sciMsgr.getFoldParent( actualline );
toggleDownUpLine = -1;
if (foldlevelnowishead) {
foldparentline = sciMsgr.getFoldParent( actualline -1 );
}
int foldlevelparent = sciMsgr.getFoldLevel( foldparentline );
if ( shifted ) {
foldlevelparent = foldlevelparent >> 16;
} else {
foldlevelparent = foldlevelparent & SC_FOLDLEVELNUMBERMASK;
}
// The or here is for nppCF with cfelse/cfelseif
if ( foldlevelparent == foldlevelPrevMask ||
( foldlevelNowMask == foldlevelparent && foldlevelnowishead ) ||
( actualline == notifyCode->line -1 && foldlevelparent == foldlevelPrevMask -1 )
) {
int indent = sciMsgr.getLineIndentation( foldparentline );
if ( sciMsgr.isAutoCActive() ) {
// store it
foldleveltomatch = notifyCode->foldLevelNow;
decrementAfterAutoC = true;
linetodecrement = actualline;
postoblame = sciMsgr.getCurrentPos();
indentationtouse = indent;
} else {
if ( langType == L_HTML ) {
// Hacky for HTML
decrementAfterPaint = true;
linetodecrement = actualline;
postoblame = sciMsgr.getCurrentPos();
indentationtouse = indent;
} else {
sciMsgr.setLineIndentation( actualline, indent );
lastFoldDownLine = curline;
}
}
}
}
} else if ( isNewLine == true && newLine == notifyCode->line && foldlevelNowMask > foldlevelPrevMask ) {
CSciMessager sciMsgr( m_nppMsgr.getCurrentScintillaWnd() );
isNewLine = false;
newLine = -1;
indentLine( notifyCode->line, false );
int indentpos = sciMsgr.getLineIndentPosition( notifyCode->line );
sciMsgr.goToPos( indentpos );
} else if ( !useNextLine && lastFoldDownLine > -1 && foldlevelNowMask > foldlevelPrevMask ) {
CSciMessager sciMsgr( m_nppMsgr.getCurrentScintillaWnd() );
int curline = sciMsgr.getLineFromPos(sciMsgr.getCurrentPos());
if ( curline == lastFoldDownLine ) {
toggleDownUpLine = curline;
}
}
}
break;
default:
break;
}
// <<< notifications from Scintilla
}
}
void IBFPlugin::OnNppReady()
{
if ( isNppWndUnicode )
{
nppOriginalWndProc = ( WNDPROC ) SetWindowLongPtrW(m_nppMsgr.getNppWnd(), GWLP_WNDPROC, ( LONG_PTR ) nppNewWndProc );
}
}
void IBFPlugin::OnNppShutdown()
{
if ( nppOriginalWndProc )
{
::SetWindowLongPtrW( m_nppMsgr.getNppWnd(), GWLP_WNDPROC, ( LONG_PTR ) nppOriginalWndProc );
}
}
void IBFPlugin::OnSciCharAdded( int ch )
{
CSciMessager sciMsgr( m_nppMsgr.getCurrentScintillaWnd() );
// lets see if we can find one
int currentpos = sciMsgr.getCurrentPos();
int line = sciMsgr.getLineFromPos( currentpos );
int eol = sciMsgr.getEOLMode();
if ( ch == '\n' || (eol == SC_EOL_CR && ch == '\r' ) )
{
indentLine( line, false );
isNewLine = true;
newLine = line;
int indentpos = sciMsgr.getLineIndentPosition( line );
sciMsgr.goToPos( indentpos );
}
else
{
isNewLine = false;
newLine = -1;
}
}
void IBFPlugin::reindentFile()
{
// See FAQ in ReadMe for details about Python!
if (langType == L_PYTHON)
{
return;
}
WaitCursor wait;
CSciMessager sciMsgr(m_nppMsgr.getCurrentScintillaWnd());
sciMsgr.beginUndoAction();
int linecount = sciMsgr.getLineCount();
sciMsgr.setLineIndentation(0, 0);
for (int line = 1; line <= linecount; line++)
{
indentLine(line, true);
}
sciMsgr.endUndoAction();
}
void IBFPlugin::indentLine( int line, bool doingwholefile )
{
CSciMessager sciMsgr( m_nppMsgr.getCurrentScintillaWnd() );
int getwidth = sciMsgr.getTabWidth();
int foldparentline = sciMsgr.getFoldParent( line );
int foldlevellastline = sciMsgr.getFoldLevel( line - 1 );
int foldlevellast = foldlevellastline & SC_FOLDLEVELNUMBERMASK;
int foldlevelcurline = sciMsgr.getFoldLevel( line );
int foldlevel = foldlevelcurline & SC_FOLDLEVELNUMBERMASK;
bool islastlinefoldparent = ( foldlevellastline & SC_FOLDLEVELHEADERFLAG) ? true : false;
int lastlinelength = sciMsgr.getLineEndPos( line - 1 ) - sciMsgr.getPosFromLine( line - 1 );
int lastlineindent = sciMsgr.getLineIndentation( line - 1 ) / getwidth;
if ( !islastlinefoldparent && toggleDownUpLine == line - 1 )
{
islastlinefoldparent = true;
}
// If the Fold Parent is the line above the new line then we indent
if ( islastlinefoldparent && foldparentline != line && lastlinelength > lastlineindent )
{
int indent = sciMsgr.getLineIndentation( foldparentline );
sciMsgr.setLineIndentation(line, indent + getwidth);
}
else if ( doingwholefile && foldlevellast > foldlevel )
{
foldparentline = sciMsgr.getFoldParent( line -1 );
int indent = sciMsgr.getLineIndentation( foldparentline );
sciMsgr.setLineIndentation( line -1, indent );
sciMsgr.setLineIndentation( line, indent );
}
else
{
int indent = sciMsgr.getLineIndentation( line -1 );
sciMsgr.setLineIndentation( line, indent );
}
}
void IBFPlugin::OnNppSetInfo( const NppData& notpadPlusData )
{
m_nppMsgr.setNppData( notpadPlusData );
isNppWndUnicode = ::IsWindowUnicode( notpadPlusData._nppHandle ) ? true : false;
lastFoldDownLine = -1;
toggleDownUpLine = -1;
newLine = -1;
isNewLine = false;
decrementAfterAutoC = false;
decrementAfterPaint = false;
postoblame = 0;
langType = L_TEXT;
}
void IBFPlugin::aboutDlg()
{
MessageBox(m_nppMsgr.getNppWnd(),
L"IndentByFold by Ben Bluemel, Frank Fesevur\n\n"
L"Version " VERSION_GIT_WSTR L"\n\n"
L"https://github.com/ffes/indentbyfold/",
L"About IndentByFold",
MB_OK);
}