1+ // NPP plugin platform for .Net v0.90 by Kasper B. Graversen etc.
12using System ;
23using System . IO ;
34using System . Text ;
89using System . Runtime . InteropServices ;
910using System . Text . RegularExpressions ;
1011using Kbg . NppPluginNET ;
12+ using Kbg . NppPluginNET . PluginInfrastructure ;
1113
1214namespace Kbg . NppPluginNET
1315{
14- /// <summary>
15- /// Integration layer as the demo app uses the pluginfiles as soft-links files.
16- /// This is different to normal plugins that would use the project template and get the files directly.
17- /// </summary>
18- class Main
19- {
20- static internal void CommandMenuInit ( )
21- {
22- Kbg . Demo . Namespace . Main . CommandMenuInit ( ) ;
23- }
24-
25- static internal void PluginCleanUp ( )
26- {
27- Kbg . Demo . Namespace . Main . PluginCleanUp ( ) ;
28- }
29-
30- static internal void SetToolBarIcon ( )
31- {
32- Kbg . Demo . Namespace . Main . SetToolBarIcon ( ) ;
33- }
34-
35- static internal void OnCharAdded ( char newChar )
16+ /// <summary>
17+ /// Integration layer as the demo app uses the pluginfiles as soft-links files.
18+ /// This is different to normal plugins that would use the project template and get the files directly.
19+ /// </summary>
20+ class Main
21+ {
22+ static internal void CommandMenuInit ( )
23+ {
24+ Kbg . Demo . Namespace . Main . CommandMenuInit ( ) ;
25+ }
26+
27+ static internal void PluginCleanUp ( )
28+ {
29+ Kbg . Demo . Namespace . Main . PluginCleanUp ( ) ;
30+ }
31+
32+ static internal void SetToolBarIcon ( )
3633 {
37- Kbg . Demo . Namespace . Main . doInsertHtmlCloseTag ( newChar ) ;
34+ Kbg . Demo . Namespace . Main . SetToolBarIcon ( ) ;
3835 }
3936
40- internal static string PluginName { get { return Kbg . Demo . Namespace . Main . PluginName ; } }
41- }
37+ public static void OnNotification ( ScNotification notification )
38+ {
39+ if ( notification . Header . code == ( uint ) SciMsg . SCN_CHARADDED )
40+ {
41+ Kbg . Demo . Namespace . Main . doInsertHtmlCloseTag ( ( char ) notification . Character ) ;
42+ }
43+ }
44+
45+ internal static string PluginName { get { return Kbg . Demo . Namespace . Main . PluginName ; } }
46+ }
4247}
4348
4449namespace Kbg . Demo . Namespace
@@ -57,9 +62,12 @@ class Main
5762 static Bitmap tbBmp = Properties . Resources . star ;
5863 static Bitmap tbBmp_tbTab = Properties . Resources . star_bmp ;
5964 static Icon tbIcon = null ;
65+ static IScintillaGateway editor = new ScintillaGateway ( PluginBase . GetCurrentScintilla ( ) ) ;
66+ static INotepadPPGateway notepad = new NotepadPPGateway ( ) ;
6067 #endregion
6168
6269 #region " Startup/CleanUp "
70+
6371 static internal void CommandMenuInit ( )
6472 {
6573 // Initialization of your plugin commands
@@ -140,43 +148,44 @@ static internal void PluginCleanUp()
140148 #region " Menu functions "
141149 static void hello ( )
142150 {
143- // Open a new document
144- Win32 . SendMessage ( PluginBase . nppData . _nppHandle , NppMsg . NPPM_MENUCOMMAND , 0 , NppMenuCmd . IDM_FILE_NEW ) ;
145- // Say hello now :
146- // Scintilla control has no Unicode mode, so we use ANSI here (marshalled as ANSI by default)
147- Win32 . SendMessage ( PluginBase . GetCurrentScintilla ( ) , SciMsg . SCI_SETTEXT , 0 , "Hello, Notepad++... from .NET!" ) ;
151+ notepad . FileNew ( ) ;
152+ editor . SetText ( "Hello, Notepad++...from.NET!" ) ;
153+ var rest = editor . GetLine ( 0 ) ;
154+ editor . SetText ( rest + rest + rest ) ;
148155 }
156+
149157 static void helloFX ( )
150158 {
151159 hello ( ) ;
152160 new Thread ( callbackHelloFX ) . Start ( ) ;
153161 }
162+
154163 static void callbackHelloFX ( )
155164 {
156- IntPtr curScintilla = PluginBase . GetCurrentScintilla ( ) ;
157- int currentZoomLevel = ( int ) Win32 . SendMessage ( curScintilla , SciMsg . SCI_GETZOOM , 0 , 0 ) ;
165+ int currentZoomLevel = editor . GetZoom ( ) ;
158166 int i = currentZoomLevel ;
159167 for ( int j = 0 ; j < 4 ; j ++ )
160168 {
161169 for ( ; i >= - 10 ; i -- )
162170 {
163- Win32 . SendMessage ( curScintilla , SciMsg . SCI_SETZOOM , i , 0 ) ;
171+ editor . SetZoomLevel ( i ) ;
164172 Thread . Sleep ( 30 ) ;
165173 }
166174 Thread . Sleep ( 100 ) ;
167175 for ( ; i <= 20 ; i ++ )
168176 {
169177 Thread . Sleep ( 30 ) ;
170- Win32 . SendMessage ( curScintilla , SciMsg . SCI_SETZOOM , i , 0 ) ;
178+ editor . SetZoomLevel ( i ) ;
171179 }
172180 Thread . Sleep ( 100 ) ;
173181 }
174182 for ( ; i >= currentZoomLevel ; i -- )
175183 {
176184 Thread . Sleep ( 30 ) ;
177- Win32 . SendMessage ( curScintilla , SciMsg . SCI_SETZOOM , i , 0 ) ;
185+ editor . SetZoomLevel ( i ) ;
178186 }
179187 }
188+
180189 static void WhatIsNpp ( )
181190 {
182191 string text2display = "Notepad++ is a free (as in \" free speech\" and also as in \" free beer\" ) " +
@@ -189,27 +198,19 @@ static void WhatIsNpp()
189198 "and reduce power consumption, resulting in a greener environment." ;
190199 new Thread ( new ParameterizedThreadStart ( callbackWhatIsNpp ) ) . Start ( text2display ) ;
191200 }
201+
192202 static void callbackWhatIsNpp ( object data )
193203 {
194204 string text2display = ( string ) data ;
195- // Open a new document
196- Win32 . SendMessage ( PluginBase . nppData . _nppHandle , NppMsg . NPPM_MENUCOMMAND , 0 , NppMenuCmd . IDM_FILE_NEW ) ;
197-
198- // Get the current scintilla
199- IntPtr curScintilla = PluginBase . GetCurrentScintilla ( ) ;
205+ notepad . FileNew ( ) ;
200206
201207 Random srand = new Random ( DateTime . Now . Millisecond ) ;
202208 int rangeMin = 0 ;
203209 int rangeMax = 250 ;
204210 for ( int i = 0 ; i < text2display . Length ; i ++ )
205211 {
206- StringBuilder charToShow = new StringBuilder ( text2display [ i ] . ToString ( ) ) ;
207-
208- int ranNum = srand . Next ( rangeMin , rangeMax ) ;
209- Thread . Sleep ( ranNum + 30 ) ;
210-
211- Win32 . SendMessage ( curScintilla , SciMsg . SCI_APPENDTEXT , 1 , charToShow ) ;
212- Win32 . SendMessage ( curScintilla , SciMsg . SCI_GOTOPOS , ( int ) Win32 . SendMessage ( curScintilla , SciMsg . SCI_GETLENGTH , 0 , 0 ) , 0 ) ;
212+ Thread . Sleep ( srand . Next ( rangeMin , rangeMax ) + 30 ) ;
213+ editor . AppendTextAndMoveCursor ( text2display [ i ] . ToString ( ) ) ;
213214 }
214215 }
215216
@@ -236,7 +237,7 @@ static void insertCurrentPath(NppMsg which)
236237 StringBuilder path = new StringBuilder ( Win32 . MAX_PATH ) ;
237238 Win32 . SendMessage ( PluginBase . nppData . _nppHandle , msg , 0 , path ) ;
238239
239- Win32 . SendMessage ( PluginBase . GetCurrentScintilla ( ) , SciMsg . SCI_REPLACESEL , 0 , path ) ;
240+ editor . ReplaceSel ( path . ToString ( ) ) ;
240241 }
241242
242243 static void insertShortDateTime ( )
@@ -249,10 +250,8 @@ static void insertLongDateTime()
249250 }
250251 static void insertDateTime ( bool longFormat )
251252 {
252- string dateTime = string . Format ( "{0} {1}" ,
253- DateTime . Now . ToShortTimeString ( ) ,
254- longFormat ? DateTime . Now . ToLongDateString ( ) : DateTime . Now . ToShortDateString ( ) ) ;
255- Win32 . SendMessage ( PluginBase . GetCurrentScintilla ( ) , SciMsg . SCI_REPLACESEL , 0 , dateTime ) ;
253+ string dateTime = string . Format ( "{0} {1}" , DateTime . Now . ToShortTimeString ( ) , longFormat ? DateTime . Now . ToLongDateString ( ) : DateTime . Now . ToShortDateString ( ) ) ;
254+ editor . ReplaceSel ( dateTime ) ;
256255 }
257256
258257 static void checkInsertHtmlCloseTag ( )
@@ -262,59 +261,62 @@ static void checkInsertHtmlCloseTag()
262261 int i = Win32 . CheckMenuItem ( Win32 . GetMenu ( PluginBase . nppData . _nppHandle ) , PluginBase . _funcItems . Items [ 9 ] . _cmdID ,
263262 Win32 . MF_BYCOMMAND | ( doCloseTag ? Win32 . MF_CHECKED : Win32 . MF_UNCHECKED ) ) ;
264263 }
264+
265+ static Regex regex = new Regex ( @"[\._\-:\w]" , RegexOptions . Compiled ) ;
266+
265267 static internal void doInsertHtmlCloseTag ( char newChar )
266268 {
267269 LangType docType = LangType . L_TEXT ;
268270 Win32 . SendMessage ( PluginBase . nppData . _nppHandle , NppMsg . NPPM_GETCURRENTLANGTYPE , 0 , ref docType ) ;
269271 bool isDocTypeHTML = ( docType == LangType . L_HTML || docType == LangType . L_XML || docType == LangType . L_PHP ) ;
270- if ( doCloseTag && isDocTypeHTML )
272+
273+ if ( ! doCloseTag || ! isDocTypeHTML )
274+ return ;
275+
276+ if ( newChar != '>' )
277+ return ;
278+
279+ int bufCapacity = 512 ;
280+ var pos = editor . GetCurrentPos ( ) ;
281+ int currentPos = pos . Value ;
282+ int beginPos = currentPos - ( bufCapacity - 1 ) ;
283+ int startPos = ( beginPos > 0 ) ? beginPos : 0 ;
284+ int size = currentPos - startPos ;
285+
286+ if ( size < 3 )
287+ return ;
288+
289+ using ( TextRange tr = new TextRange ( startPos , currentPos , bufCapacity ) )
271290 {
272- if ( newChar == '>' )
291+ editor . GetTextRange ( tr ) ;
292+ string buf = tr . lpstrText ;
293+
294+ if ( buf [ size - 2 ] == '/' )
295+ return ;
296+
297+ int pCur = size - 2 ;
298+ while ( ( pCur > 0 ) && ( buf [ pCur ] != '<' ) && ( buf [ pCur ] != '>' ) )
299+ pCur -- ;
300+
301+ if ( buf [ pCur ] == '<' )
273302 {
274- int bufCapacity = 512 ;
275- IntPtr hCurrentEditView = PluginBase . GetCurrentScintilla ( ) ;
276- int currentPos = ( int ) Win32 . SendMessage ( hCurrentEditView , SciMsg . SCI_GETCURRENTPOS , 0 , 0 ) ;
277- int beginPos = currentPos - ( bufCapacity - 1 ) ;
278- int startPos = ( beginPos > 0 ) ? beginPos : 0 ;
279- int size = currentPos - startPos ;
280-
281- if ( size >= 3 )
303+ pCur ++ ;
304+
305+ var insertString = new StringBuilder ( "</" ) ;
306+
307+ while ( regex . IsMatch ( buf [ pCur ] . ToString ( ) ) )
308+ {
309+ insertString . Append ( buf [ pCur ] ) ;
310+ pCur ++ ;
311+ }
312+ insertString . Append ( '>' ) ;
313+
314+ if ( insertString . Length > 3 )
282315 {
283- using ( Sci_TextRange tr = new Sci_TextRange ( startPos , currentPos , bufCapacity ) )
284- {
285- Win32 . SendMessage ( hCurrentEditView , SciMsg . SCI_GETTEXTRANGE , 0 , tr . NativePointer ) ;
286- string buf = tr . lpstrText ;
287-
288- if ( buf [ size - 2 ] != '/' )
289- {
290- StringBuilder insertString = new StringBuilder ( "</" ) ;
291-
292- int pCur = size - 2 ;
293- for ( ; ( pCur > 0 ) && ( buf [ pCur ] != '<' ) && ( buf [ pCur ] != '>' ) ; )
294- pCur -- ;
295-
296- if ( buf [ pCur ] == '<' )
297- {
298- pCur ++ ;
299-
300- Regex regex = new Regex ( @"[\._\-:\w]" ) ;
301- while ( regex . IsMatch ( buf [ pCur ] . ToString ( ) ) )
302- {
303- insertString . Append ( buf [ pCur ] ) ;
304- pCur ++ ;
305- }
306- insertString . Append ( '>' ) ;
307-
308- if ( insertString . Length > 3 )
309- {
310- Win32 . SendMessage ( hCurrentEditView , SciMsg . SCI_BEGINUNDOACTION , 0 , 0 ) ;
311- Win32 . SendMessage ( hCurrentEditView , SciMsg . SCI_REPLACESEL , 0 , insertString ) ;
312- Win32 . SendMessage ( hCurrentEditView , SciMsg . SCI_SETSEL , currentPos , currentPos ) ;
313- Win32 . SendMessage ( hCurrentEditView , SciMsg . SCI_ENDUNDOACTION , 0 , 0 ) ;
314- }
315- }
316- }
317- }
316+ editor . BeginUndoAction ( ) ;
317+ editor . ReplaceSel ( insertString . ToString ( ) ) ;
318+ editor . SetSel ( pos , pos ) ;
319+ editor . EndUndoAction ( ) ;
318320 }
319321 }
320322 }
@@ -363,7 +365,7 @@ static void DockableDlgDemo()
363365 // You can create your own non dockable dialog - in this case you don't nedd this demonstration.
364366 if ( frmGoToLine == null )
365367 {
366- frmGoToLine = new frmGoToLine ( ) ;
368+ frmGoToLine = new frmGoToLine ( editor ) ;
367369
368370 using ( Bitmap newBmp = new Bitmap ( 16 , 16 ) )
369371 {
0 commit comments