Hello,
script needs to use both npp views, 2nd is were you type the regular expression.
Code commented
importre# import regular expression moduleeditor1.indicSetStyle(10,INDICATORSTYLE.CONTAINER)# used to color whole match - odd lineseditor1.indicSetFore(10,(95,215,184))# the coloreditor1.indicSetAlpha(10,55)# alpha settingseditor1.indicSetOutlineAlpha(8,255)# outliningeditor1.indicSetUnder(10,True)# draw under the texteditor1.indicSetStyle(9,INDICATORSTYLE.CONTAINER)# used to color whole match - even lineseditor1.indicSetFore(9,(195,215,184))editor1.indicSetAlpha(9,55)editor1.indicSetOutlineAlpha(8,255)editor1.indicSetUnder(9,True)editor1.indicSetStyle(8,INDICATORSTYLE.ROUNDBOX)# used for sub matcheseditor1.indicSetFore(8,(100,215,100))editor1.indicSetAlpha(8,55)editor1.indicSetOutlineAlpha(8,255)editor1.indicSetUnder(8,True)isOdd=False# used as even/odd line identifierdefmatch_found(m):globalisOdd# global, because we modify itifm.lastindex>0:# >0 = how many submatches do we haveforiinrange(0,m.lastindex+1):# loop over itifi==0:# match 0 is always the whole matcheditor1.setIndicatorCurrent(9ifisOddelse10)# set indicator for whole matcheditor1.indicatorFillRange(m.span(0)[0],m.span(0)[1]-m.span(0)[0])# draw indicatorisOdd=FalseifisOddelseTrue# set even/odd identifier - next whole match gets coloured differentelse:editor1.setIndicatorCurrent(8)# set indicator for sub matcheseditor1.indicatorFillRange(m.span(i)[0],m.span(i)[1]-m.span(i)[0])# draw itelse:# no sub matches editor1.setIndicatorCurrent(8)# set the same indicator as normally used in sub matcheseditor1.indicatorFillRange(m.span(0)[0],m.span(0)[1]-m.span(0)[0])# guess what :-) yes, draw itdefclear_indicator():# clear all indicators bylength=editor1.getTextLength()# calculating length of documentforiinrange(8,11):# and looping overeditor1.setIndicatorCurrent(i)# each indicator toeditor1.indicatorClearRange(0,length)# clear the rangedefregex():# here the regex startsclear_indicator()# first have a clear view ;-)pattern=editor2.getLine(0).rstrip()# next, get the pattern for the second view and cut of line endingstry:# try itifeditor2.getLine(2)[22:23]=='I':# is it a case insensitive search?editor1.research(pattern,match_found,re.IGNORECASE)# then call research with the ignore case flagelse:# otherwiseeditor1.research(pattern,match_found)# call without flagexcept:pass# is needed to catch incorrect regular expressionsdefRegexTester_CHARADDED(args):# callback which gets called each time when char is added in editorregex()# calls itself regex functiondefRegexTester_UPDATEUI(args):# callback gets called and emulates a CHARDELETE notificationifargs['updated']==3:# is a bit of a hack butregex()# seems to workifeditor2.getProperty('RegexTester_running')!='1':# if the script isn't currently runningeditor.callback(RegexTester_CHARADDED,[SCINTILLANOTIFICATION.CHARADDED])# register the callbacks charaddeditor.callback(RegexTester_UPDATEUI,[SCINTILLANOTIFICATION.UPDATEUI])# and emulated chardeleteifeditor2.getProperty('RegexTester_running')=='0':# this checks if script was already running, stopped and restarted againeditor2.replace('RegexTester inActive','RegexTester isActive')# add the status info to second viewelse:# no, this is the first time we run the script soeditor2.appendText('\r\n\r\nRegexTester isActive [i] i=sensitive, I=insesitive')# add the status info to second vieweditor2.setProperty('RegexTester_running','1')# and set the running identifiereditor2.setFocus(True)# give the second view the focuseditor2.gotoLine(0)# and jump to line 1else:# the script runs already so this call is used toeditor.clearCallbacks([SCINTILLANOTIFICATION.CHARADDED])# clear the callback charadded andeditor.clearCallbacks([SCINTILLANOTIFICATION.UPDATEUI])# emulated chardeletededitor2.setProperty('RegexTester_running','0')# set info that script isn't runningeditor2.replace('RegexTester isActive','RegexTester inActive')# add the status info to second viewclear_indicator()# clear all indicatorseditor1.setFocus(True)# and give first view the focus. Have fun
Cheers
Claudia
Last edit: CFrank 2016-03-19
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Dave,
of course you are correct (and corrected) but I don't know why/how this happens.
I've posted the same on npp community which is correct. Hmmm - must be sleepwalking me. ;-)
Cheers
Claudia
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Is the script run from Python Script folder as other one?
What do you mean from folder?
You start npp and then you goto plugins->python script->new script,
give it a meaningful name and put the code into it. save it.
Open both views and run the script by using plugins->python script->scripts->YOUR_NAME -> done
The animate gif Dave postet should explain its uasage pretty well.
Concerning the console, Plugins->Python Script->Show Console.
Cheers
Claudia
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
script needs to use both npp views, 2nd is were you type the regular expression.
Code commented
Cheers
Claudia
Last edit: CFrank 2016-03-19
Wow! This is really really awesome!
Think
re import
should beimport re
as the first line.https://twitter.com/bruderstein/status/711138614432894976
Hi Dave,
of course you are correct (and corrected) but I don't know why/how this happens.
I've posted the same on npp community which is correct. Hmmm - must be sleepwalking me. ;-)
Cheers
Claudia
As Python Script ? Not working...
What is not working?
Do you have any error in the console?
Cheers
Claudia
Is the script run from Python Script folder as other one?
I didn't see any console appear, seems to not run when launch it...
Cheers
What do you mean from folder?
You start npp and then you goto plugins->python script->new script,
give it a meaningful name and put the code into it. save it.
Open both views and run the script by using plugins->python script->scripts->YOUR_NAME -> done
The animate gif Dave postet should explain its uasage pretty well.
Concerning the console, Plugins->Python Script->Show Console.
Cheers
Claudia
I did not make the second view, I see that work, but now all text is marked ;)
Just use an empty line and coloring should go away ;-)
Cheers
Claudia
Ok, thank you, this is the most interactive code I ever see on NPP, magic !
Marvellous,inspirational, useful - thanks!
An updated version (V2) is available here
https://sourceforge.net/p/npppythonscript/discussion/1199074/thread/f59511a4/