TextView.cs 189 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563
  1. #nullable enable
  2. // TextView.cs: multi-line text editing
  3. using System.Diagnostics;
  4. using System.Globalization;
  5. using System.Runtime.CompilerServices;
  6. using System.Text.Json.Serialization;
  7. using Terminal.Gui.Resources;
  8. namespace Terminal.Gui;
  9. /// <summary>
  10. /// Represents a single row/column within the <see cref="TextView"/>. Includes the glyph and the
  11. /// foreground/background colors.
  12. /// </summary>
  13. [DebuggerDisplay ("{ColorSchemeDebuggerDisplay}")]
  14. public class RuneCell : IEquatable<RuneCell>
  15. {
  16. /// <summary>The <see cref="Terminal.Gui.ColorScheme"/> color sets to draw the glyph with.</summary>
  17. [JsonConverter (typeof (ColorSchemeJsonConverter))]
  18. public ColorScheme? ColorScheme { get; set; }
  19. /// <summary>The glyph to draw.</summary>
  20. [JsonConverter (typeof (RuneJsonConverter))]
  21. public Rune Rune { get; set; }
  22. private string ColorSchemeDebuggerDisplay => ToString ();
  23. /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
  24. /// <param name="other">An object to compare with this object.</param>
  25. /// <returns>
  26. /// <see langword="true"/> if the current object is equal to the <paramref name="other"/> parameter; otherwise,
  27. /// <see langword="false"/>.
  28. /// </returns>
  29. public bool Equals (RuneCell? other) { return other is { } && Rune.Equals (other.Rune) && ColorScheme == other.ColorScheme; }
  30. /// <summary>Returns a string that represents the current object.</summary>
  31. /// <returns>A string that represents the current object.</returns>
  32. public override string ToString ()
  33. {
  34. string colorSchemeStr = ColorScheme?.ToString () ?? "null";
  35. return $"U+{Rune.Value:X4} '{Rune.ToString ()}'; {colorSchemeStr}";
  36. }
  37. }
  38. internal class TextModel
  39. {
  40. private List<List<RuneCell>> _lines = new ();
  41. private (Point startPointToFind, Point currentPointToFind, bool found) _toFind;
  42. /// <summary>The number of text lines in the model</summary>
  43. public int Count => _lines.Count;
  44. public string? FilePath { get; set; }
  45. /// <summary>Adds a line to the model at the specified position.</summary>
  46. /// <param name="pos">Line number where the line will be inserted.</param>
  47. /// <param name="cells">The line of text and color, as a List of RuneCell.</param>
  48. public void AddLine (int pos, List<RuneCell> cells) { _lines.Insert (pos, cells); }
  49. public bool CloseFile ()
  50. {
  51. if (FilePath is null)
  52. {
  53. throw new ArgumentNullException (nameof (FilePath));
  54. }
  55. FilePath = null;
  56. _lines = new List<List<RuneCell>> ();
  57. return true;
  58. }
  59. public List<List<RuneCell>> GetAllLines () { return _lines; }
  60. /// <summary>Returns the specified line as a List of Rune</summary>
  61. /// <returns>The line.</returns>
  62. /// <param name="line">Line number to retrieve.</param>
  63. public List<RuneCell> GetLine (int line)
  64. {
  65. if (_lines.Count > 0)
  66. {
  67. if (line < Count)
  68. {
  69. return _lines [line];
  70. }
  71. return _lines [Count - 1];
  72. }
  73. _lines.Add (new List<RuneCell> ());
  74. return _lines [0];
  75. }
  76. /// <summary>Returns the maximum line length of the visible lines.</summary>
  77. /// <param name="first">The first line.</param>
  78. /// <param name="last">The last line.</param>
  79. /// <param name="tabWidth">The tab width.</param>
  80. public int GetMaxVisibleLine (int first, int last, int tabWidth)
  81. {
  82. var maxLength = 0;
  83. last = last < _lines.Count ? last : _lines.Count;
  84. for (int i = first; i < last; i++)
  85. {
  86. List<RuneCell> line = GetLine (i);
  87. int tabSum = line.Sum (c => c.Rune.Value == '\t' ? Math.Max (tabWidth - 1, 0) : 0);
  88. int l = line.Count + tabSum;
  89. if (l > maxLength)
  90. {
  91. maxLength = l;
  92. }
  93. }
  94. return maxLength;
  95. }
  96. public event EventHandler? LinesLoaded;
  97. public bool LoadFile (string file)
  98. {
  99. FilePath = file ?? throw new ArgumentNullException (nameof (file));
  100. using (FileStream stream = File.OpenRead (file))
  101. {
  102. LoadStream (stream);
  103. return true;
  104. }
  105. }
  106. public void LoadListRuneCells (List<List<RuneCell>> cellsList, ColorScheme? colorScheme)
  107. {
  108. _lines = cellsList;
  109. SetColorSchemes (colorScheme);
  110. OnLinesLoaded ();
  111. }
  112. public void LoadRuneCells (List<RuneCell> cells, ColorScheme? colorScheme)
  113. {
  114. _lines = ToRuneCells (cells);
  115. SetColorSchemes (colorScheme);
  116. OnLinesLoaded ();
  117. }
  118. public void LoadStream (Stream input)
  119. {
  120. if (input is null)
  121. {
  122. throw new ArgumentNullException (nameof (input));
  123. }
  124. _lines = new List<List<RuneCell>> ();
  125. var buff = new BufferedStream (input);
  126. int v;
  127. List<byte> line = new ();
  128. var wasNewLine = false;
  129. while ((v = buff.ReadByte ()) != -1)
  130. {
  131. if (v == 13)
  132. {
  133. continue;
  134. }
  135. if (v == 10)
  136. {
  137. Append (line);
  138. line.Clear ();
  139. wasNewLine = true;
  140. continue;
  141. }
  142. line.Add ((byte)v);
  143. wasNewLine = false;
  144. }
  145. if (line.Count > 0 || wasNewLine)
  146. {
  147. Append (line);
  148. }
  149. buff.Dispose ();
  150. OnLinesLoaded ();
  151. }
  152. public void LoadString (string content)
  153. {
  154. _lines = StringToLinesOfRuneCells (content);
  155. OnLinesLoaded ();
  156. }
  157. /// <summary>Removes the line at the specified position</summary>
  158. /// <param name="pos">Position.</param>
  159. public void RemoveLine (int pos)
  160. {
  161. if (_lines.Count > 0)
  162. {
  163. if (_lines.Count == 1 && _lines [0].Count == 0)
  164. {
  165. return;
  166. }
  167. _lines.RemoveAt (pos);
  168. }
  169. }
  170. public void ReplaceLine (int pos, List<RuneCell> runes)
  171. {
  172. if (_lines.Count > 0 && pos < _lines.Count)
  173. {
  174. _lines [pos] = new List<RuneCell> (runes);
  175. }
  176. else if (_lines.Count == 0 || (_lines.Count > 0 && pos >= _lines.Count))
  177. {
  178. _lines.Add (runes);
  179. }
  180. }
  181. // Splits a string into a List that contains a List<RuneCell> for each line
  182. public static List<List<RuneCell>> StringToLinesOfRuneCells (string content, ColorScheme? colorScheme = null)
  183. {
  184. List<RuneCell> cells = content.EnumerateRunes ()
  185. .Select (x => new RuneCell { Rune = x, ColorScheme = colorScheme })
  186. .ToList ();
  187. return SplitNewLines (cells);
  188. }
  189. /// <summary>Converts the string into a <see cref="List{RuneCell}"/>.</summary>
  190. /// <param name="str">The string to convert.</param>
  191. /// <param name="colorScheme">The <see cref="ColorScheme"/> to use.</param>
  192. /// <returns></returns>
  193. public static List<RuneCell> ToRuneCellList (string str, ColorScheme? colorScheme = null)
  194. {
  195. List<RuneCell> cells = new ();
  196. foreach (Rune rune in str.EnumerateRunes ())
  197. {
  198. cells.Add (new RuneCell { Rune = rune, ColorScheme = colorScheme });
  199. }
  200. return cells;
  201. }
  202. public override string ToString ()
  203. {
  204. var sb = new StringBuilder ();
  205. for (var i = 0; i < _lines.Count; i++)
  206. {
  207. sb.Append (ToString (_lines [i]));
  208. if (i + 1 < _lines.Count)
  209. {
  210. sb.AppendLine ();
  211. }
  212. }
  213. return sb.ToString ();
  214. }
  215. /// <summary>Converts a <see cref="RuneCell"/> generic collection into a string.</summary>
  216. /// <param name="cells">The enumerable cell to convert.</param>
  217. /// <returns></returns>
  218. public static string ToString (IEnumerable<RuneCell> cells)
  219. {
  220. var str = string.Empty;
  221. foreach (RuneCell cell in cells)
  222. {
  223. str += cell.Rune.ToString ();
  224. }
  225. return str;
  226. }
  227. public (int col, int row)? WordBackward (int fromCol, int fromRow)
  228. {
  229. if (fromRow == 0 && fromCol == 0)
  230. {
  231. return null;
  232. }
  233. int col = Math.Max (fromCol - 1, 0);
  234. int row = fromRow;
  235. try
  236. {
  237. RuneCell? cell = RuneAt (col, row);
  238. Rune rune;
  239. if (cell is { })
  240. {
  241. rune = cell.Rune;
  242. }
  243. else
  244. {
  245. if (col > 0)
  246. {
  247. return (col, row);
  248. }
  249. if (col == 0 && row > 0)
  250. {
  251. row--;
  252. List<RuneCell> line = GetLine (row);
  253. return (line.Count, row);
  254. }
  255. return null;
  256. }
  257. RuneType runeType = GetRuneType (rune);
  258. int lastValidCol = IsSameRuneType (rune, runeType) && (Rune.IsLetterOrDigit (rune) || Rune.IsPunctuation (rune) || Rune.IsSymbol (rune))
  259. ? col
  260. : -1;
  261. void ProcMovePrev (ref int nCol, ref int nRow, Rune nRune)
  262. {
  263. if (Rune.IsWhiteSpace (nRune))
  264. {
  265. while (MovePrev (ref nCol, ref nRow, out nRune))
  266. {
  267. if (Rune.IsLetterOrDigit (nRune) || Rune.IsPunctuation (nRune) || Rune.IsSymbol (nRune))
  268. {
  269. lastValidCol = nCol;
  270. if (runeType == RuneType.IsWhiteSpace || runeType == RuneType.IsUnknow)
  271. {
  272. runeType = GetRuneType (nRune);
  273. }
  274. break;
  275. }
  276. }
  277. if (nRow != fromRow && (Rune.IsLetterOrDigit (nRune) || Rune.IsPunctuation (nRune) || Rune.IsSymbol (nRune)))
  278. {
  279. if (lastValidCol > -1)
  280. {
  281. nCol = lastValidCol;
  282. }
  283. return;
  284. }
  285. while (MovePrev (ref nCol, ref nRow, out nRune))
  286. {
  287. if (!Rune.IsLetterOrDigit (nRune) && !Rune.IsPunctuation (nRune) && !Rune.IsSymbol (nRune))
  288. {
  289. break;
  290. }
  291. if (nRow != fromRow)
  292. {
  293. break;
  294. }
  295. lastValidCol =
  296. (IsSameRuneType (nRune, runeType) && Rune.IsLetterOrDigit (nRune)) || Rune.IsPunctuation (nRune) || Rune.IsSymbol (nRune)
  297. ? nCol
  298. : lastValidCol;
  299. }
  300. if (lastValidCol > -1)
  301. {
  302. nCol = lastValidCol;
  303. nRow = fromRow;
  304. }
  305. }
  306. else
  307. {
  308. if (!MovePrev (ref nCol, ref nRow, out nRune))
  309. {
  310. return;
  311. }
  312. List<RuneCell> line = GetLine (nRow);
  313. if (nCol == 0
  314. && nRow == fromRow
  315. && (Rune.IsLetterOrDigit (line [0].Rune) || Rune.IsPunctuation (line [0].Rune) || Rune.IsSymbol (line [0].Rune)))
  316. {
  317. return;
  318. }
  319. lastValidCol =
  320. (IsSameRuneType (nRune, runeType) && Rune.IsLetterOrDigit (nRune)) || Rune.IsPunctuation (nRune) || Rune.IsSymbol (nRune)
  321. ? nCol
  322. : lastValidCol;
  323. if (lastValidCol > -1 && Rune.IsWhiteSpace (nRune))
  324. {
  325. nCol = lastValidCol;
  326. return;
  327. }
  328. if (fromRow != nRow)
  329. {
  330. nCol = line.Count;
  331. return;
  332. }
  333. ProcMovePrev (ref nCol, ref nRow, nRune);
  334. }
  335. }
  336. ProcMovePrev (ref col, ref row, rune);
  337. if (fromCol != col || fromRow != row)
  338. {
  339. return (col, row);
  340. }
  341. return null;
  342. }
  343. catch (Exception)
  344. {
  345. return null;
  346. }
  347. }
  348. public (int col, int row)? WordForward (int fromCol, int fromRow)
  349. {
  350. if (fromRow == _lines.Count - 1 && fromCol == GetLine (_lines.Count - 1).Count)
  351. {
  352. return null;
  353. }
  354. int col = fromCol;
  355. int row = fromRow;
  356. try
  357. {
  358. Rune rune = RuneAt (col, row).Rune;
  359. RuneType runeType = GetRuneType (rune);
  360. int lastValidCol = IsSameRuneType (rune, runeType) && (Rune.IsLetterOrDigit (rune) || Rune.IsPunctuation (rune) || Rune.IsSymbol (rune))
  361. ? col
  362. : -1;
  363. void ProcMoveNext (ref int nCol, ref int nRow, Rune nRune)
  364. {
  365. if (Rune.IsWhiteSpace (nRune))
  366. {
  367. while (MoveNext (ref nCol, ref nRow, out nRune))
  368. {
  369. if (Rune.IsLetterOrDigit (nRune) || Rune.IsPunctuation (nRune) || Rune.IsSymbol (nRune))
  370. {
  371. lastValidCol = nCol;
  372. return;
  373. }
  374. }
  375. if (nRow != fromRow && (Rune.IsLetterOrDigit (nRune) || Rune.IsPunctuation (nRune) || Rune.IsSymbol (nRune)))
  376. {
  377. if (lastValidCol > -1)
  378. {
  379. nCol = lastValidCol;
  380. }
  381. return;
  382. }
  383. while (MoveNext (ref nCol, ref nRow, out nRune))
  384. {
  385. if (!Rune.IsLetterOrDigit (nRune) && !Rune.IsPunctuation (nRune) && !Rune.IsSymbol (nRune))
  386. {
  387. break;
  388. }
  389. if (nRow != fromRow)
  390. {
  391. break;
  392. }
  393. lastValidCol =
  394. (IsSameRuneType (nRune, runeType) && Rune.IsLetterOrDigit (nRune)) || Rune.IsPunctuation (nRune) || Rune.IsSymbol (nRune)
  395. ? nCol
  396. : lastValidCol;
  397. }
  398. if (lastValidCol > -1)
  399. {
  400. nCol = lastValidCol;
  401. nRow = fromRow;
  402. }
  403. }
  404. else
  405. {
  406. if (!MoveNext (ref nCol, ref nRow, out nRune))
  407. {
  408. return;
  409. }
  410. if (!IsSameRuneType (nRune, runeType) && !Rune.IsWhiteSpace (nRune))
  411. {
  412. return;
  413. }
  414. List<RuneCell> line = GetLine (nRow);
  415. if (nCol == line.Count
  416. && nRow == fromRow
  417. && (Rune.IsLetterOrDigit (line [0].Rune) || Rune.IsPunctuation (line [0].Rune) || Rune.IsSymbol (line [0].Rune)))
  418. {
  419. return;
  420. }
  421. lastValidCol =
  422. (IsSameRuneType (nRune, runeType) && Rune.IsLetterOrDigit (nRune)) || Rune.IsPunctuation (nRune) || Rune.IsSymbol (nRune)
  423. ? nCol
  424. : lastValidCol;
  425. if (fromRow != nRow)
  426. {
  427. nCol = 0;
  428. return;
  429. }
  430. ProcMoveNext (ref nCol, ref nRow, nRune);
  431. }
  432. }
  433. ProcMoveNext (ref col, ref row, rune);
  434. if (fromCol != col || fromRow != row)
  435. {
  436. return (col, row);
  437. }
  438. return null;
  439. }
  440. catch (Exception)
  441. {
  442. return null;
  443. }
  444. }
  445. internal static int CalculateLeftColumn (List<RuneCell> t, int start, int end, int width, int tabWidth = 0)
  446. {
  447. List<Rune> runes = new ();
  448. foreach (RuneCell cell in t)
  449. {
  450. runes.Add (cell.Rune);
  451. }
  452. return CalculateLeftColumn (runes, start, end, width, tabWidth);
  453. }
  454. // Returns the left column in a range of the string.
  455. internal static int CalculateLeftColumn (List<Rune> t, int start, int end, int width, int tabWidth = 0)
  456. {
  457. if (t is null || t.Count == 0)
  458. {
  459. return 0;
  460. }
  461. var size = 0;
  462. int tcount = end > t.Count - 1 ? t.Count - 1 : end;
  463. var col = 0;
  464. for (int i = tcount; i >= 0; i--)
  465. {
  466. Rune rune = t [i];
  467. size += rune.GetColumns ();
  468. if (rune.Value == '\t')
  469. {
  470. size += tabWidth + 1;
  471. }
  472. if (size > width)
  473. {
  474. if (col + width == end)
  475. {
  476. col++;
  477. }
  478. break;
  479. }
  480. if ((end < t.Count && col > 0 && start < end && col == start) || end - col == width - 1)
  481. {
  482. break;
  483. }
  484. col = i;
  485. }
  486. return col;
  487. }
  488. internal static (int size, int length) DisplaySize (
  489. List<RuneCell> t,
  490. int start = -1,
  491. int end = -1,
  492. bool checkNextRune = true,
  493. int tabWidth = 0
  494. )
  495. {
  496. List<Rune> runes = new ();
  497. foreach (RuneCell cell in t)
  498. {
  499. runes.Add (cell.Rune);
  500. }
  501. return DisplaySize (runes, start, end, checkNextRune, tabWidth);
  502. }
  503. // Returns the size and length in a range of the string.
  504. internal static (int size, int length) DisplaySize (
  505. List<Rune> t,
  506. int start = -1,
  507. int end = -1,
  508. bool checkNextRune = true,
  509. int tabWidth = 0
  510. )
  511. {
  512. if (t is null || t.Count == 0)
  513. {
  514. return (0, 0);
  515. }
  516. var size = 0;
  517. var len = 0;
  518. int tcount = end == -1 ? t.Count :
  519. end > t.Count ? t.Count : end;
  520. int i = start == -1 ? 0 : start;
  521. for (; i < tcount; i++)
  522. {
  523. Rune rune = t [i];
  524. size += rune.GetColumns ();
  525. len += rune.GetEncodingLength (Encoding.Unicode);
  526. if (rune.Value == '\t')
  527. {
  528. size += tabWidth + 1;
  529. len += tabWidth - 1;
  530. }
  531. if (checkNextRune && i == tcount - 1 && t.Count > tcount && IsWideRune (t [i + 1], tabWidth, out int s, out int l))
  532. {
  533. size += s;
  534. len += l;
  535. }
  536. }
  537. bool IsWideRune (Rune r, int tWidth, out int s, out int l)
  538. {
  539. s = r.GetColumns ();
  540. l = r.GetEncodingLength ();
  541. if (r.Value == '\t')
  542. {
  543. s += tWidth + 1;
  544. l += tWidth - 1;
  545. }
  546. return s > 1;
  547. }
  548. return (size, len);
  549. }
  550. internal (Point current, bool found) FindNextText (
  551. string text,
  552. out bool gaveFullTurn,
  553. bool matchCase = false,
  554. bool matchWholeWord = false
  555. )
  556. {
  557. if (text is null || _lines.Count == 0)
  558. {
  559. gaveFullTurn = false;
  560. return (Point.Empty, false);
  561. }
  562. if (_toFind.found)
  563. {
  564. _toFind.currentPointToFind.X++;
  565. }
  566. (Point current, bool found) foundPos = GetFoundNextTextPoint (
  567. text,
  568. _lines.Count,
  569. matchCase,
  570. matchWholeWord,
  571. _toFind.currentPointToFind
  572. );
  573. if (!foundPos.found && _toFind.currentPointToFind != _toFind.startPointToFind)
  574. {
  575. foundPos = GetFoundNextTextPoint (
  576. text,
  577. _toFind.startPointToFind.Y + 1,
  578. matchCase,
  579. matchWholeWord,
  580. Point.Empty
  581. );
  582. }
  583. gaveFullTurn = ApplyToFind (foundPos);
  584. return foundPos;
  585. }
  586. internal (Point current, bool found) FindPreviousText (
  587. string text,
  588. out bool gaveFullTurn,
  589. bool matchCase = false,
  590. bool matchWholeWord = false
  591. )
  592. {
  593. if (text is null || _lines.Count == 0)
  594. {
  595. gaveFullTurn = false;
  596. return (Point.Empty, false);
  597. }
  598. if (_toFind.found)
  599. {
  600. _toFind.currentPointToFind.X++;
  601. }
  602. int linesCount = _toFind.currentPointToFind.IsEmpty ? _lines.Count - 1 : _toFind.currentPointToFind.Y;
  603. (Point current, bool found) foundPos = GetFoundPreviousTextPoint (
  604. text,
  605. linesCount,
  606. matchCase,
  607. matchWholeWord,
  608. _toFind.currentPointToFind
  609. );
  610. if (!foundPos.found && _toFind.currentPointToFind != _toFind.startPointToFind)
  611. {
  612. foundPos = GetFoundPreviousTextPoint (
  613. text,
  614. _lines.Count - 1,
  615. matchCase,
  616. matchWholeWord,
  617. new Point (_lines [_lines.Count - 1].Count, _lines.Count)
  618. );
  619. }
  620. gaveFullTurn = ApplyToFind (foundPos);
  621. return foundPos;
  622. }
  623. internal static int GetColFromX (List<RuneCell> t, int start, int x, int tabWidth = 0)
  624. {
  625. List<Rune> runes = new ();
  626. foreach (RuneCell cell in t)
  627. {
  628. runes.Add (cell.Rune);
  629. }
  630. return GetColFromX (runes, start, x, tabWidth);
  631. }
  632. internal static int GetColFromX (List<Rune> t, int start, int x, int tabWidth = 0)
  633. {
  634. if (x < 0)
  635. {
  636. return x;
  637. }
  638. int size = start;
  639. int pX = x + start;
  640. for (int i = start; i < t.Count; i++)
  641. {
  642. Rune r = t [i];
  643. size += r.GetColumns ();
  644. if (r.Value == '\t')
  645. {
  646. size += tabWidth + 1;
  647. }
  648. if (i == pX || size > pX)
  649. {
  650. return i - start;
  651. }
  652. }
  653. return t.Count - start;
  654. }
  655. internal (Point current, bool found) ReplaceAllText (
  656. string text,
  657. bool matchCase = false,
  658. bool matchWholeWord = false,
  659. string? textToReplace = null
  660. )
  661. {
  662. var found = false;
  663. var pos = Point.Empty;
  664. for (var i = 0; i < _lines.Count; i++)
  665. {
  666. List<RuneCell> x = _lines [i];
  667. string txt = GetText (x);
  668. string matchText = !matchCase ? text.ToUpper () : text;
  669. int col = txt.IndexOf (matchText);
  670. while (col > -1)
  671. {
  672. if (matchWholeWord && !MatchWholeWord (txt, matchText, col))
  673. {
  674. if (col + 1 > txt.Length)
  675. {
  676. break;
  677. }
  678. col = txt.IndexOf (matchText, col + 1);
  679. continue;
  680. }
  681. if (col > -1)
  682. {
  683. if (!found)
  684. {
  685. found = true;
  686. }
  687. _lines [i] = ToRuneCellList (ReplaceText (x, textToReplace!, matchText, col));
  688. x = _lines [i];
  689. txt = GetText (x);
  690. pos = new Point (col, i);
  691. col += textToReplace!.Length - matchText.Length;
  692. }
  693. if (col < 0 || col + 1 > txt.Length)
  694. {
  695. break;
  696. }
  697. col = txt.IndexOf (matchText, col + 1);
  698. }
  699. }
  700. string GetText (List<RuneCell> x)
  701. {
  702. string txt = ToString (x);
  703. if (!matchCase)
  704. {
  705. txt = txt.ToUpper ();
  706. }
  707. return txt;
  708. }
  709. return (pos, found);
  710. }
  711. /// <summary>Redefine column and line tracking.</summary>
  712. /// <param name="point">Contains the column and line.</param>
  713. internal void ResetContinuousFind (Point point)
  714. {
  715. _toFind.startPointToFind = _toFind.currentPointToFind = point;
  716. _toFind.found = false;
  717. }
  718. internal static bool SetCol (ref int col, int width, int cols)
  719. {
  720. if (col + cols <= width)
  721. {
  722. col += cols;
  723. return true;
  724. }
  725. return false;
  726. }
  727. // Turns the string into cells, this does not split the
  728. // contents on a newline if it is present.
  729. internal static List<RuneCell> StringToRuneCells (string str, ColorScheme? colorScheme = null)
  730. {
  731. List<RuneCell> cells = new ();
  732. foreach (Rune rune in str.ToRunes ())
  733. {
  734. cells.Add (new RuneCell { Rune = rune, ColorScheme = colorScheme });
  735. }
  736. return cells;
  737. }
  738. internal static List<RuneCell> ToRuneCells (IEnumerable<Rune> runes, ColorScheme? colorScheme = null)
  739. {
  740. List<RuneCell> cells = new ();
  741. foreach (Rune rune in runes)
  742. {
  743. cells.Add (new RuneCell { Rune = rune, ColorScheme = colorScheme });
  744. }
  745. return cells;
  746. }
  747. private void Append (List<byte> line)
  748. {
  749. var str = StringExtensions.ToString (line.ToArray ());
  750. _lines.Add (StringToRuneCells (str));
  751. }
  752. private bool ApplyToFind ((Point current, bool found) foundPos)
  753. {
  754. var gaveFullTurn = false;
  755. if (foundPos.found)
  756. {
  757. _toFind.currentPointToFind = foundPos.current;
  758. if (_toFind.found && _toFind.currentPointToFind == _toFind.startPointToFind)
  759. {
  760. gaveFullTurn = true;
  761. }
  762. if (!_toFind.found)
  763. {
  764. _toFind.startPointToFind = _toFind.currentPointToFind = foundPos.current;
  765. _toFind.found = foundPos.found;
  766. }
  767. }
  768. return gaveFullTurn;
  769. }
  770. private (Point current, bool found) GetFoundNextTextPoint (
  771. string text,
  772. int linesCount,
  773. bool matchCase,
  774. bool matchWholeWord,
  775. Point start
  776. )
  777. {
  778. for (int i = start.Y; i < linesCount; i++)
  779. {
  780. List<RuneCell> x = _lines [i];
  781. string txt = ToString (x);
  782. if (!matchCase)
  783. {
  784. txt = txt.ToUpper ();
  785. }
  786. string matchText = !matchCase ? text.ToUpper () : text;
  787. int col = txt.IndexOf (matchText, Math.Min (start.X, txt.Length));
  788. if (col > -1 && matchWholeWord && !MatchWholeWord (txt, matchText, col))
  789. {
  790. continue;
  791. }
  792. if (col > -1 && ((i == start.Y && col >= start.X) || i > start.Y) && txt.Contains (matchText))
  793. {
  794. return (new Point (col, i), true);
  795. }
  796. if (col == -1 && start.X > 0)
  797. {
  798. start.X = 0;
  799. }
  800. }
  801. return (Point.Empty, false);
  802. }
  803. private (Point current, bool found) GetFoundPreviousTextPoint (
  804. string text,
  805. int linesCount,
  806. bool matchCase,
  807. bool matchWholeWord,
  808. Point start
  809. )
  810. {
  811. for (int i = linesCount; i >= 0; i--)
  812. {
  813. List<RuneCell> x = _lines [i];
  814. string txt = ToString (x);
  815. if (!matchCase)
  816. {
  817. txt = txt.ToUpper ();
  818. }
  819. if (start.Y != i)
  820. {
  821. start.X = Math.Max (x.Count - 1, 0);
  822. }
  823. string matchText = !matchCase ? text.ToUpper () : text;
  824. int col = txt.LastIndexOf (matchText, _toFind.found ? start.X - 1 : start.X);
  825. if (col > -1 && matchWholeWord && !MatchWholeWord (txt, matchText, col))
  826. {
  827. continue;
  828. }
  829. if (col > -1 && ((i <= linesCount && col <= start.X) || i < start.Y) && txt.Contains (matchText))
  830. {
  831. return (new Point (col, i), true);
  832. }
  833. }
  834. return (Point.Empty, false);
  835. }
  836. private RuneType GetRuneType (Rune rune)
  837. {
  838. if (Rune.IsSymbol (rune))
  839. {
  840. return RuneType.IsSymbol;
  841. }
  842. if (Rune.IsWhiteSpace (rune))
  843. {
  844. return RuneType.IsWhiteSpace;
  845. }
  846. if (Rune.IsLetterOrDigit (rune))
  847. {
  848. return RuneType.IsLetterOrDigit;
  849. }
  850. if (Rune.IsPunctuation (rune))
  851. {
  852. return RuneType.IsPunctuation;
  853. }
  854. return RuneType.IsUnknow;
  855. }
  856. private bool IsSameRuneType (Rune newRune, RuneType runeType)
  857. {
  858. RuneType rt = GetRuneType (newRune);
  859. return rt == runeType;
  860. }
  861. private bool MatchWholeWord (string source, string matchText, int index = 0)
  862. {
  863. if (string.IsNullOrEmpty (source) || string.IsNullOrEmpty (matchText))
  864. {
  865. return false;
  866. }
  867. string txt = matchText.Trim ();
  868. int start = index > 0 ? index - 1 : 0;
  869. int end = index + txt.Length;
  870. if ((start == 0 || Rune.IsWhiteSpace ((Rune)source [start])) && (end == source.Length || Rune.IsWhiteSpace ((Rune)source [end])))
  871. {
  872. return true;
  873. }
  874. return false;
  875. }
  876. private bool MoveNext (ref int col, ref int row, out Rune rune)
  877. {
  878. List<RuneCell> line = GetLine (row);
  879. if (col + 1 < line.Count)
  880. {
  881. col++;
  882. rune = line [col].Rune;
  883. if (col + 1 == line.Count && !Rune.IsLetterOrDigit (rune) && !Rune.IsWhiteSpace (line [col - 1].Rune))
  884. {
  885. col++;
  886. }
  887. return true;
  888. }
  889. if (col + 1 == line.Count)
  890. {
  891. col++;
  892. }
  893. while (row + 1 < Count)
  894. {
  895. col = 0;
  896. row++;
  897. line = GetLine (row);
  898. if (line.Count > 0)
  899. {
  900. rune = line [0].Rune;
  901. return true;
  902. }
  903. }
  904. rune = default (Rune);
  905. return false;
  906. }
  907. private bool MovePrev (ref int col, ref int row, out Rune rune)
  908. {
  909. List<RuneCell> line = GetLine (row);
  910. if (col > 0)
  911. {
  912. col--;
  913. rune = line [col].Rune;
  914. return true;
  915. }
  916. if (row == 0)
  917. {
  918. rune = default (Rune);
  919. return false;
  920. }
  921. while (row > 0)
  922. {
  923. row--;
  924. line = GetLine (row);
  925. col = line.Count - 1;
  926. if (col >= 0)
  927. {
  928. rune = line [col].Rune;
  929. return true;
  930. }
  931. }
  932. rune = default (Rune);
  933. return false;
  934. }
  935. private void OnLinesLoaded () { LinesLoaded?.Invoke (this, EventArgs.Empty); }
  936. private string ReplaceText (List<RuneCell> source, string textToReplace, string matchText, int col)
  937. {
  938. string origTxt = ToString (source);
  939. (_, int len) = DisplaySize (source, 0, col, false);
  940. (_, int len2) = DisplaySize (source, col, col + matchText.Length, false);
  941. (_, int len3) = DisplaySize (source, col + matchText.Length, origTxt.GetRuneCount (), false);
  942. return origTxt [..len] + textToReplace + origTxt.Substring (len + len2, len3);
  943. }
  944. private RuneCell RuneAt (int col, int row)
  945. {
  946. List<RuneCell> line = GetLine (row);
  947. if (line.Count > 0)
  948. {
  949. return line [col > line.Count - 1 ? line.Count - 1 : col];
  950. }
  951. return default (RuneCell)!;
  952. }
  953. private void SetColorSchemes (ColorScheme? colorScheme)
  954. {
  955. foreach (List<RuneCell> line in _lines)
  956. {
  957. foreach (RuneCell cell in line)
  958. {
  959. cell.ColorScheme ??= colorScheme;
  960. }
  961. }
  962. }
  963. private static List<List<RuneCell>> SplitNewLines (List<RuneCell> cells)
  964. {
  965. List<List<RuneCell>> lines = new ();
  966. int start = 0, i = 0;
  967. var hasCR = false;
  968. // ASCII code 13 = Carriage Return.
  969. // ASCII code 10 = Line Feed.
  970. for (; i < cells.Count; i++)
  971. {
  972. if (cells [i].Rune.Value == 13)
  973. {
  974. hasCR = true;
  975. continue;
  976. }
  977. if (cells [i].Rune.Value == 10)
  978. {
  979. if (i - start > 0)
  980. {
  981. lines.Add (cells.GetRange (start, hasCR ? i - 1 - start : i - start));
  982. }
  983. else
  984. {
  985. lines.Add (StringToRuneCells (string.Empty));
  986. }
  987. start = i + 1;
  988. hasCR = false;
  989. }
  990. }
  991. if (i - start >= 0)
  992. {
  993. lines.Add (cells.GetRange (start, i - start));
  994. }
  995. return lines;
  996. }
  997. private static List<List<RuneCell>> ToRuneCells (List<RuneCell> cells) { return SplitNewLines (cells); }
  998. private enum RuneType
  999. {
  1000. IsSymbol,
  1001. IsWhiteSpace,
  1002. IsLetterOrDigit,
  1003. IsPunctuation,
  1004. IsUnknow
  1005. }
  1006. }
  1007. internal partial class HistoryText
  1008. {
  1009. public enum LineStatus
  1010. {
  1011. Original,
  1012. Replaced,
  1013. Removed,
  1014. Added
  1015. }
  1016. private readonly List<HistoryTextItem> _historyTextItems = new ();
  1017. private int _idxHistoryText = -1;
  1018. private string? _originalText;
  1019. public bool HasHistoryChanges => _idxHistoryText > -1;
  1020. public bool IsFromHistory { get; private set; }
  1021. public void Add (List<List<RuneCell>> lines, Point curPos, LineStatus lineStatus = LineStatus.Original)
  1022. {
  1023. if (lineStatus == LineStatus.Original && _historyTextItems.Count > 0 && _historyTextItems.Last ().LineStatus == LineStatus.Original)
  1024. {
  1025. return;
  1026. }
  1027. if (lineStatus == LineStatus.Replaced && _historyTextItems.Count > 0 && _historyTextItems.Last ().LineStatus == LineStatus.Replaced)
  1028. {
  1029. return;
  1030. }
  1031. if (_historyTextItems.Count == 0 && lineStatus != LineStatus.Original)
  1032. {
  1033. throw new ArgumentException ("The first item must be the original.");
  1034. }
  1035. if (_idxHistoryText >= 0 && _idxHistoryText + 1 < _historyTextItems.Count)
  1036. {
  1037. _historyTextItems.RemoveRange (
  1038. _idxHistoryText + 1,
  1039. _historyTextItems.Count - _idxHistoryText - 1
  1040. );
  1041. }
  1042. _historyTextItems.Add (new HistoryTextItem (lines, curPos, lineStatus));
  1043. _idxHistoryText++;
  1044. }
  1045. public event EventHandler<HistoryTextItem>? ChangeText;
  1046. public void Clear (string text)
  1047. {
  1048. _historyTextItems.Clear ();
  1049. _idxHistoryText = -1;
  1050. _originalText = text;
  1051. OnChangeText (null);
  1052. }
  1053. public bool IsDirty (string text) { return _originalText != text; }
  1054. public void Redo ()
  1055. {
  1056. if (_historyTextItems?.Count > 0 && _idxHistoryText < _historyTextItems.Count - 1)
  1057. {
  1058. IsFromHistory = true;
  1059. _idxHistoryText++;
  1060. var historyTextItem = new HistoryTextItem (_historyTextItems [_idxHistoryText]) { IsUndoing = false };
  1061. ProcessChanges (ref historyTextItem);
  1062. IsFromHistory = false;
  1063. }
  1064. }
  1065. public void ReplaceLast (List<List<RuneCell>> lines, Point curPos, LineStatus lineStatus)
  1066. {
  1067. HistoryTextItem? found = _historyTextItems.FindLast (x => x.LineStatus == lineStatus);
  1068. if (found is { })
  1069. {
  1070. found.Lines = lines;
  1071. found.CursorPosition = curPos;
  1072. }
  1073. }
  1074. public void Undo ()
  1075. {
  1076. if (_historyTextItems?.Count > 0 && _idxHistoryText > 0)
  1077. {
  1078. IsFromHistory = true;
  1079. _idxHistoryText--;
  1080. var historyTextItem = new HistoryTextItem (_historyTextItems [_idxHistoryText]) { IsUndoing = true };
  1081. ProcessChanges (ref historyTextItem);
  1082. IsFromHistory = false;
  1083. }
  1084. }
  1085. private void OnChangeText (HistoryTextItem? lines) { ChangeText?.Invoke (this, lines!); }
  1086. private void ProcessChanges (ref HistoryTextItem historyTextItem)
  1087. {
  1088. if (historyTextItem.IsUndoing)
  1089. {
  1090. if (_idxHistoryText - 1 > -1
  1091. && (_historyTextItems [_idxHistoryText - 1].LineStatus == LineStatus.Added
  1092. || _historyTextItems [_idxHistoryText - 1].LineStatus == LineStatus.Removed
  1093. || (historyTextItem.LineStatus == LineStatus.Replaced && _historyTextItems [_idxHistoryText - 1].LineStatus == LineStatus.Original)))
  1094. {
  1095. _idxHistoryText--;
  1096. while (_historyTextItems [_idxHistoryText].LineStatus == LineStatus.Added
  1097. && _historyTextItems [_idxHistoryText - 1].LineStatus == LineStatus.Removed)
  1098. {
  1099. _idxHistoryText--;
  1100. }
  1101. historyTextItem = new HistoryTextItem (_historyTextItems [_idxHistoryText]);
  1102. historyTextItem.IsUndoing = true;
  1103. historyTextItem.FinalCursorPosition = historyTextItem.CursorPosition;
  1104. }
  1105. if (historyTextItem.LineStatus == LineStatus.Removed && _historyTextItems [_idxHistoryText + 1].LineStatus == LineStatus.Added)
  1106. {
  1107. historyTextItem.RemovedOnAdded =
  1108. new HistoryTextItem (_historyTextItems [_idxHistoryText + 1]);
  1109. }
  1110. if ((historyTextItem.LineStatus == LineStatus.Added && _historyTextItems [_idxHistoryText - 1].LineStatus == LineStatus.Original)
  1111. || (historyTextItem.LineStatus == LineStatus.Removed && _historyTextItems [_idxHistoryText - 1].LineStatus == LineStatus.Original)
  1112. || (historyTextItem.LineStatus == LineStatus.Added && _historyTextItems [_idxHistoryText - 1].LineStatus == LineStatus.Removed))
  1113. {
  1114. if (!historyTextItem.Lines [0]
  1115. .SequenceEqual (_historyTextItems [_idxHistoryText - 1].Lines [0])
  1116. && historyTextItem.CursorPosition == _historyTextItems [_idxHistoryText - 1].CursorPosition)
  1117. {
  1118. historyTextItem.Lines [0] =
  1119. new List<RuneCell> (_historyTextItems [_idxHistoryText - 1].Lines [0]);
  1120. }
  1121. if (historyTextItem.LineStatus == LineStatus.Added && _historyTextItems [_idxHistoryText - 1].LineStatus == LineStatus.Removed)
  1122. {
  1123. historyTextItem.FinalCursorPosition =
  1124. _historyTextItems [_idxHistoryText - 2].CursorPosition;
  1125. }
  1126. else
  1127. {
  1128. historyTextItem.FinalCursorPosition =
  1129. _historyTextItems [_idxHistoryText - 1].CursorPosition;
  1130. }
  1131. }
  1132. else
  1133. {
  1134. historyTextItem.FinalCursorPosition = historyTextItem.CursorPosition;
  1135. }
  1136. OnChangeText (historyTextItem);
  1137. while (_historyTextItems [_idxHistoryText].LineStatus == LineStatus.Removed
  1138. || _historyTextItems [_idxHistoryText].LineStatus == LineStatus.Added)
  1139. {
  1140. _idxHistoryText--;
  1141. }
  1142. }
  1143. else if (!historyTextItem.IsUndoing)
  1144. {
  1145. if (_idxHistoryText + 1 < _historyTextItems.Count
  1146. && (historyTextItem.LineStatus == LineStatus.Original
  1147. || _historyTextItems [_idxHistoryText + 1].LineStatus == LineStatus.Added
  1148. || _historyTextItems [_idxHistoryText + 1].LineStatus == LineStatus.Removed))
  1149. {
  1150. _idxHistoryText++;
  1151. historyTextItem = new HistoryTextItem (_historyTextItems [_idxHistoryText]);
  1152. historyTextItem.IsUndoing = false;
  1153. historyTextItem.FinalCursorPosition = historyTextItem.CursorPosition;
  1154. }
  1155. if (historyTextItem.LineStatus == LineStatus.Added && _historyTextItems [_idxHistoryText - 1].LineStatus == LineStatus.Removed)
  1156. {
  1157. historyTextItem.RemovedOnAdded =
  1158. new HistoryTextItem (_historyTextItems [_idxHistoryText - 1]);
  1159. }
  1160. if ((historyTextItem.LineStatus == LineStatus.Removed && _historyTextItems [_idxHistoryText + 1].LineStatus == LineStatus.Replaced)
  1161. || (historyTextItem.LineStatus == LineStatus.Removed && _historyTextItems [_idxHistoryText + 1].LineStatus == LineStatus.Original)
  1162. || (historyTextItem.LineStatus == LineStatus.Added && _historyTextItems [_idxHistoryText + 1].LineStatus == LineStatus.Replaced))
  1163. {
  1164. if (historyTextItem.LineStatus == LineStatus.Removed
  1165. && !historyTextItem.Lines [0]
  1166. .SequenceEqual (_historyTextItems [_idxHistoryText + 1].Lines [0]))
  1167. {
  1168. historyTextItem.Lines [0] =
  1169. new List<RuneCell> (_historyTextItems [_idxHistoryText + 1].Lines [0]);
  1170. }
  1171. historyTextItem.FinalCursorPosition =
  1172. _historyTextItems [_idxHistoryText + 1].CursorPosition;
  1173. }
  1174. else
  1175. {
  1176. historyTextItem.FinalCursorPosition = historyTextItem.CursorPosition;
  1177. }
  1178. OnChangeText (historyTextItem);
  1179. while (_historyTextItems [_idxHistoryText].LineStatus == LineStatus.Removed
  1180. || _historyTextItems [_idxHistoryText].LineStatus == LineStatus.Added)
  1181. {
  1182. _idxHistoryText++;
  1183. }
  1184. }
  1185. }
  1186. }
  1187. internal class WordWrapManager
  1188. {
  1189. private int _frameWidth;
  1190. private bool _isWrapModelRefreshing;
  1191. private List<WrappedLine> _wrappedModelLines = new ();
  1192. public WordWrapManager (TextModel model) { Model = model; }
  1193. public TextModel Model { get; private set; }
  1194. public void AddLine (int row, int col)
  1195. {
  1196. int modelRow = GetModelLineFromWrappedLines (row);
  1197. int modelCol = GetModelColFromWrappedLines (row, col);
  1198. List<RuneCell> line = GetCurrentLine (modelRow);
  1199. int restCount = line.Count - modelCol;
  1200. List<RuneCell> rest = line.GetRange (modelCol, restCount);
  1201. line.RemoveRange (modelCol, restCount);
  1202. Model.AddLine (modelRow + 1, rest);
  1203. _isWrapModelRefreshing = true;
  1204. WrapModel (_frameWidth, out _, out _, out _, out _, modelRow + 1);
  1205. _isWrapModelRefreshing = false;
  1206. }
  1207. public int GetModelColFromWrappedLines (int line, int col)
  1208. {
  1209. if (_wrappedModelLines?.Count == 0)
  1210. {
  1211. return 0;
  1212. }
  1213. int modelLine = GetModelLineFromWrappedLines (line);
  1214. int firstLine = _wrappedModelLines.IndexOf (r => r.ModelLine == modelLine);
  1215. var modelCol = 0;
  1216. for (int i = firstLine; i <= Math.Min (line, _wrappedModelLines!.Count - 1); i++)
  1217. {
  1218. WrappedLine wLine = _wrappedModelLines [i];
  1219. if (i < line)
  1220. {
  1221. modelCol += wLine.ColWidth;
  1222. }
  1223. else
  1224. {
  1225. modelCol += col;
  1226. }
  1227. }
  1228. return modelCol;
  1229. }
  1230. public int GetModelLineFromWrappedLines (int line)
  1231. {
  1232. return _wrappedModelLines.Count > 0
  1233. ? _wrappedModelLines [Math.Min (
  1234. line,
  1235. _wrappedModelLines.Count - 1
  1236. )].ModelLine
  1237. : 0;
  1238. }
  1239. public int GetWrappedLineColWidth (int line, int col, WordWrapManager wrapManager)
  1240. {
  1241. if (_wrappedModelLines?.Count == 0)
  1242. {
  1243. return 0;
  1244. }
  1245. List<WrappedLine> wModelLines = wrapManager._wrappedModelLines;
  1246. int modelLine = GetModelLineFromWrappedLines (line);
  1247. int firstLine = _wrappedModelLines.IndexOf (r => r.ModelLine == modelLine);
  1248. var modelCol = 0;
  1249. var colWidthOffset = 0;
  1250. int i = firstLine;
  1251. while (modelCol < col)
  1252. {
  1253. WrappedLine wLine = _wrappedModelLines! [i];
  1254. WrappedLine wLineToCompare = wModelLines [i];
  1255. if (wLine.ModelLine != modelLine || wLineToCompare.ModelLine != modelLine)
  1256. {
  1257. break;
  1258. }
  1259. modelCol += Math.Max (wLine.ColWidth, wLineToCompare.ColWidth);
  1260. colWidthOffset += wLine.ColWidth - wLineToCompare.ColWidth;
  1261. if (modelCol > col)
  1262. {
  1263. modelCol += col - modelCol;
  1264. }
  1265. i++;
  1266. }
  1267. return modelCol - colWidthOffset;
  1268. }
  1269. public bool Insert (int row, int col, RuneCell cell)
  1270. {
  1271. List<RuneCell> line = GetCurrentLine (GetModelLineFromWrappedLines (row));
  1272. line.Insert (GetModelColFromWrappedLines (row, col), cell);
  1273. if (line.Count > _frameWidth)
  1274. {
  1275. return true;
  1276. }
  1277. return false;
  1278. }
  1279. public bool RemoveAt (int row, int col)
  1280. {
  1281. int modelRow = GetModelLineFromWrappedLines (row);
  1282. List<RuneCell> line = GetCurrentLine (modelRow);
  1283. int modelCol = GetModelColFromWrappedLines (row, col);
  1284. if (modelCol > line.Count)
  1285. {
  1286. Model.RemoveLine (modelRow);
  1287. RemoveAt (row, 0);
  1288. return false;
  1289. }
  1290. if (modelCol < line.Count)
  1291. {
  1292. line.RemoveAt (modelCol);
  1293. }
  1294. if (line.Count > _frameWidth || (row + 1 < _wrappedModelLines.Count && _wrappedModelLines [row + 1].ModelLine == modelRow))
  1295. {
  1296. return true;
  1297. }
  1298. return false;
  1299. }
  1300. public bool RemoveLine (int row, int col, out bool lineRemoved, bool forward = true)
  1301. {
  1302. lineRemoved = false;
  1303. int modelRow = GetModelLineFromWrappedLines (row);
  1304. List<RuneCell> line = GetCurrentLine (modelRow);
  1305. int modelCol = GetModelColFromWrappedLines (row, col);
  1306. if (modelCol == 0 && line.Count == 0)
  1307. {
  1308. Model.RemoveLine (modelRow);
  1309. return false;
  1310. }
  1311. if (modelCol < line.Count)
  1312. {
  1313. if (forward)
  1314. {
  1315. line.RemoveAt (modelCol);
  1316. return true;
  1317. }
  1318. if (modelCol - 1 > -1)
  1319. {
  1320. line.RemoveAt (modelCol - 1);
  1321. return true;
  1322. }
  1323. }
  1324. lineRemoved = true;
  1325. if (forward)
  1326. {
  1327. if (modelRow + 1 == Model.Count)
  1328. {
  1329. return false;
  1330. }
  1331. List<RuneCell> nextLine = Model.GetLine (modelRow + 1);
  1332. line.AddRange (nextLine);
  1333. Model.RemoveLine (modelRow + 1);
  1334. if (line.Count > _frameWidth)
  1335. {
  1336. return true;
  1337. }
  1338. }
  1339. else
  1340. {
  1341. if (modelRow == 0)
  1342. {
  1343. return false;
  1344. }
  1345. List<RuneCell> prevLine = Model.GetLine (modelRow - 1);
  1346. prevLine.AddRange (line);
  1347. Model.RemoveLine (modelRow);
  1348. if (prevLine.Count > _frameWidth)
  1349. {
  1350. return true;
  1351. }
  1352. }
  1353. return false;
  1354. }
  1355. public bool RemoveRange (int row, int index, int count)
  1356. {
  1357. int modelRow = GetModelLineFromWrappedLines (row);
  1358. List<RuneCell> line = GetCurrentLine (modelRow);
  1359. int modelCol = GetModelColFromWrappedLines (row, index);
  1360. try
  1361. {
  1362. line.RemoveRange (modelCol, count);
  1363. }
  1364. catch (Exception)
  1365. {
  1366. return false;
  1367. }
  1368. return true;
  1369. }
  1370. public List<List<RuneCell>> ToListRune (List<string> textList)
  1371. {
  1372. List<List<RuneCell>> runesList = new ();
  1373. foreach (string text in textList)
  1374. {
  1375. runesList.Add (TextModel.ToRuneCellList (text));
  1376. }
  1377. return runesList;
  1378. }
  1379. public void UpdateModel (
  1380. TextModel model,
  1381. out int nRow,
  1382. out int nCol,
  1383. out int nStartRow,
  1384. out int nStartCol,
  1385. int row,
  1386. int col,
  1387. int startRow,
  1388. int startCol,
  1389. bool preserveTrailingSpaces
  1390. )
  1391. {
  1392. _isWrapModelRefreshing = true;
  1393. Model = model;
  1394. WrapModel (
  1395. _frameWidth,
  1396. out nRow,
  1397. out nCol,
  1398. out nStartRow,
  1399. out nStartCol,
  1400. row,
  1401. col,
  1402. startRow,
  1403. startCol,
  1404. 0,
  1405. preserveTrailingSpaces
  1406. );
  1407. _isWrapModelRefreshing = false;
  1408. }
  1409. public TextModel WrapModel (
  1410. int width,
  1411. out int nRow,
  1412. out int nCol,
  1413. out int nStartRow,
  1414. out int nStartCol,
  1415. int row = 0,
  1416. int col = 0,
  1417. int startRow = 0,
  1418. int startCol = 0,
  1419. int tabWidth = 0,
  1420. bool preserveTrailingSpaces = true
  1421. )
  1422. {
  1423. _frameWidth = width;
  1424. int modelRow = _isWrapModelRefreshing ? row : GetModelLineFromWrappedLines (row);
  1425. int modelCol = _isWrapModelRefreshing ? col : GetModelColFromWrappedLines (row, col);
  1426. int modelStartRow = _isWrapModelRefreshing ? startRow : GetModelLineFromWrappedLines (startRow);
  1427. int modelStartCol =
  1428. _isWrapModelRefreshing ? startCol : GetModelColFromWrappedLines (startRow, startCol);
  1429. var wrappedModel = new TextModel ();
  1430. var lines = 0;
  1431. nRow = 0;
  1432. nCol = 0;
  1433. nStartRow = 0;
  1434. nStartCol = 0;
  1435. bool isRowAndColSetted = row == 0 && col == 0;
  1436. bool isStartRowAndColSetted = startRow == 0 && startCol == 0;
  1437. List<WrappedLine> wModelLines = new ();
  1438. for (var i = 0; i < Model.Count; i++)
  1439. {
  1440. List<RuneCell> line = Model.GetLine (i);
  1441. List<List<RuneCell>> wrappedLines = ToListRune (
  1442. TextFormatter.Format (
  1443. TextModel.ToString (line),
  1444. width,
  1445. TextAlignment.Left,
  1446. true,
  1447. preserveTrailingSpaces,
  1448. tabWidth
  1449. )
  1450. );
  1451. var sumColWidth = 0;
  1452. for (var j = 0; j < wrappedLines.Count; j++)
  1453. {
  1454. List<RuneCell> wrapLine = wrappedLines [j];
  1455. if (!isRowAndColSetted && modelRow == i)
  1456. {
  1457. if (nCol + wrapLine.Count <= modelCol)
  1458. {
  1459. nCol += wrapLine.Count;
  1460. nRow = lines;
  1461. if (nCol == modelCol)
  1462. {
  1463. nCol = wrapLine.Count;
  1464. isRowAndColSetted = true;
  1465. }
  1466. else if (j == wrappedLines.Count - 1)
  1467. {
  1468. nCol = wrapLine.Count - j + modelCol - nCol;
  1469. isRowAndColSetted = true;
  1470. }
  1471. }
  1472. else
  1473. {
  1474. int offset = nCol + wrapLine.Count - modelCol;
  1475. nCol = wrapLine.Count - offset;
  1476. nRow = lines;
  1477. isRowAndColSetted = true;
  1478. }
  1479. }
  1480. if (!isStartRowAndColSetted && modelStartRow == i)
  1481. {
  1482. if (nStartCol + wrapLine.Count <= modelStartCol)
  1483. {
  1484. nStartCol += wrapLine.Count;
  1485. nStartRow = lines;
  1486. if (nStartCol == modelStartCol)
  1487. {
  1488. nStartCol = wrapLine.Count;
  1489. isStartRowAndColSetted = true;
  1490. }
  1491. else if (j == wrappedLines.Count - 1)
  1492. {
  1493. nStartCol = wrapLine.Count - j + modelStartCol - nStartCol;
  1494. isStartRowAndColSetted = true;
  1495. }
  1496. }
  1497. else
  1498. {
  1499. int offset = nStartCol + wrapLine.Count - modelStartCol;
  1500. nStartCol = wrapLine.Count - offset;
  1501. nStartRow = lines;
  1502. isStartRowAndColSetted = true;
  1503. }
  1504. }
  1505. for (int k = j; k < wrapLine.Count; k++)
  1506. {
  1507. wrapLine [k].ColorScheme = line [k].ColorScheme;
  1508. }
  1509. wrappedModel.AddLine (lines, wrapLine);
  1510. sumColWidth += wrapLine.Count;
  1511. var wrappedLine = new WrappedLine
  1512. {
  1513. ModelLine = i, Row = lines, RowIndex = j, ColWidth = wrapLine.Count
  1514. };
  1515. wModelLines.Add (wrappedLine);
  1516. lines++;
  1517. }
  1518. }
  1519. _wrappedModelLines = wModelLines;
  1520. return wrappedModel;
  1521. }
  1522. private List<RuneCell> GetCurrentLine (int row) { return Model.GetLine (row); }
  1523. private class WrappedLine
  1524. {
  1525. public int ColWidth;
  1526. public int ModelLine;
  1527. public int Row;
  1528. public int RowIndex;
  1529. }
  1530. }
  1531. /// <summary>Multi-line text editing <see cref="View"/>.</summary>
  1532. /// <remarks>
  1533. /// <para>
  1534. /// <see cref="TextView"/> provides a multi-line text editor. Users interact with it with the standard Windows,
  1535. /// Mac, and Linux (Emacs) commands.
  1536. /// </para>
  1537. /// <list type="table">
  1538. /// <listheader>
  1539. /// <term>Shortcut</term> <description>Action performed</description>
  1540. /// </listheader>
  1541. /// <item>
  1542. /// <term>Left cursor, Control-b</term> <description>Moves the editing point left.</description>
  1543. /// </item>
  1544. /// <item>
  1545. /// <term>Right cursor, Control-f</term> <description>Moves the editing point right.</description>
  1546. /// </item>
  1547. /// <item>
  1548. /// <term>Alt-b</term> <description>Moves one word back.</description>
  1549. /// </item>
  1550. /// <item>
  1551. /// <term>Alt-f</term> <description>Moves one word forward.</description>
  1552. /// </item>
  1553. /// <item>
  1554. /// <term>Up cursor, Control-p</term> <description>Moves the editing point one line up.</description>
  1555. /// </item>
  1556. /// <item>
  1557. /// <term>Down cursor, Control-n</term> <description>Moves the editing point one line down</description>
  1558. /// </item>
  1559. /// <item>
  1560. /// <term>Home key, Control-a</term> <description>Moves the cursor to the beginning of the line.</description>
  1561. /// </item>
  1562. /// <item>
  1563. /// <term>End key, Control-e</term> <description>Moves the cursor to the end of the line.</description>
  1564. /// </item>
  1565. /// <item>
  1566. /// <term>Control-Home</term> <description>Scrolls to the first line and moves the cursor there.</description>
  1567. /// </item>
  1568. /// <item>
  1569. /// <term>Control-End</term> <description>Scrolls to the last line and moves the cursor there.</description>
  1570. /// </item>
  1571. /// <item>
  1572. /// <term>Delete, Control-d</term> <description>Deletes the character in front of the cursor.</description>
  1573. /// </item>
  1574. /// <item>
  1575. /// <term>Backspace</term> <description>Deletes the character behind the cursor.</description>
  1576. /// </item>
  1577. /// <item>
  1578. /// <term>Control-k</term>
  1579. /// <description>
  1580. /// Deletes the text until the end of the line and replaces the kill buffer with the deleted text.
  1581. /// You can paste this text in a different place by using Control-y.
  1582. /// </description>
  1583. /// </item>
  1584. /// <item>
  1585. /// <term>Control-y</term>
  1586. /// <description>Pastes the content of the kill ring into the current position.</description>
  1587. /// </item>
  1588. /// <item>
  1589. /// <term>Alt-d</term>
  1590. /// <description>
  1591. /// Deletes the word above the cursor and adds it to the kill ring. You can paste the contents of
  1592. /// the kill ring with Control-y.
  1593. /// </description>
  1594. /// </item>
  1595. /// <item>
  1596. /// <term>Control-q</term>
  1597. /// <description>
  1598. /// Quotes the next input character, to prevent the normal processing of key handling to take
  1599. /// place.
  1600. /// </description>
  1601. /// </item>
  1602. /// </list>
  1603. /// </remarks>
  1604. public class TextView : View
  1605. {
  1606. private readonly HistoryText _historyText = new ();
  1607. private bool _allowsReturn = true;
  1608. private bool _allowsTab = true;
  1609. private int _bottomOffset, _rightOffset;
  1610. private bool _clickWithSelecting;
  1611. // The column we are tracking, or -1 if we are not tracking any column
  1612. private int _columnTrack = -1;
  1613. private bool _continuousFind;
  1614. private bool _copyWithoutSelection;
  1615. private string? _currentCaller;
  1616. private CultureInfo? _currentCulture;
  1617. private CursorVisibility _desiredCursorVisibility = CursorVisibility.Default;
  1618. private bool _isButtonShift;
  1619. private bool _isDrawing;
  1620. private bool _isReadOnly;
  1621. private bool _lastWasKill;
  1622. private int _leftColumn;
  1623. private TextModel _model = new ();
  1624. private bool _multiline = true;
  1625. private CursorVisibility _savedCursorVisibility;
  1626. private Dim? _savedHeight;
  1627. private int _selectionStartColumn, _selectionStartRow;
  1628. private bool _shiftSelecting;
  1629. private int _tabWidth = 4;
  1630. private int _topRow;
  1631. private bool _wordWrap;
  1632. private WordWrapManager? _wrapManager;
  1633. private bool _wrapNeeded;
  1634. /// <summary>
  1635. /// Initializes a <see cref="TextView"/> on the specified area, with dimensions controlled with the X, Y, Width
  1636. /// and Height properties.
  1637. /// </summary>
  1638. public TextView ()
  1639. {
  1640. CanFocus = true;
  1641. Used = true;
  1642. _model.LinesLoaded += Model_LinesLoaded!;
  1643. _historyText.ChangeText += HistoryText_ChangeText!;
  1644. Initialized += TextView_Initialized!;
  1645. LayoutComplete += TextView_LayoutComplete;
  1646. // Things this view knows how to do
  1647. AddCommand (
  1648. Command.PageDown,
  1649. () =>
  1650. {
  1651. ProcessPageDown ();
  1652. return true;
  1653. }
  1654. );
  1655. AddCommand (
  1656. Command.PageDownExtend,
  1657. () =>
  1658. {
  1659. ProcessPageDownExtend ();
  1660. return true;
  1661. }
  1662. );
  1663. AddCommand (
  1664. Command.PageUp,
  1665. () =>
  1666. {
  1667. ProcessPageUp ();
  1668. return true;
  1669. }
  1670. );
  1671. AddCommand (
  1672. Command.PageUpExtend,
  1673. () =>
  1674. {
  1675. ProcessPageUpExtend ();
  1676. return true;
  1677. }
  1678. );
  1679. AddCommand (
  1680. Command.LineDown,
  1681. () =>
  1682. {
  1683. ProcessMoveDown ();
  1684. return true;
  1685. }
  1686. );
  1687. AddCommand (
  1688. Command.LineDownExtend,
  1689. () =>
  1690. {
  1691. ProcessMoveDownExtend ();
  1692. return true;
  1693. }
  1694. );
  1695. AddCommand (
  1696. Command.LineUp,
  1697. () =>
  1698. {
  1699. ProcessMoveUp ();
  1700. return true;
  1701. }
  1702. );
  1703. AddCommand (
  1704. Command.LineUpExtend,
  1705. () =>
  1706. {
  1707. ProcessMoveUpExtend ();
  1708. return true;
  1709. }
  1710. );
  1711. AddCommand (Command.Right, () => ProcessMoveRight ());
  1712. AddCommand (
  1713. Command.RightExtend,
  1714. () =>
  1715. {
  1716. ProcessMoveRightExtend ();
  1717. return true;
  1718. }
  1719. );
  1720. AddCommand (Command.Left, () => ProcessMoveLeft ());
  1721. AddCommand (
  1722. Command.LeftExtend,
  1723. () =>
  1724. {
  1725. ProcessMoveLeftExtend ();
  1726. return true;
  1727. }
  1728. );
  1729. AddCommand (
  1730. Command.DeleteCharLeft,
  1731. () =>
  1732. {
  1733. ProcessDeleteCharLeft ();
  1734. return true;
  1735. }
  1736. );
  1737. AddCommand (
  1738. Command.StartOfLine,
  1739. () =>
  1740. {
  1741. ProcessMoveStartOfLine ();
  1742. return true;
  1743. }
  1744. );
  1745. AddCommand (
  1746. Command.StartOfLineExtend,
  1747. () =>
  1748. {
  1749. ProcessMoveStartOfLineExtend ();
  1750. return true;
  1751. }
  1752. );
  1753. AddCommand (
  1754. Command.DeleteCharRight,
  1755. () =>
  1756. {
  1757. ProcessDeleteCharRight ();
  1758. return true;
  1759. }
  1760. );
  1761. AddCommand (
  1762. Command.EndOfLine,
  1763. () =>
  1764. {
  1765. ProcessMoveEndOfLine ();
  1766. return true;
  1767. }
  1768. );
  1769. AddCommand (
  1770. Command.EndOfLineExtend,
  1771. () =>
  1772. {
  1773. ProcessMoveEndOfLineExtend ();
  1774. return true;
  1775. }
  1776. );
  1777. AddCommand (
  1778. Command.CutToEndLine,
  1779. () =>
  1780. {
  1781. KillToEndOfLine ();
  1782. return true;
  1783. }
  1784. );
  1785. AddCommand (
  1786. Command.CutToStartLine,
  1787. () =>
  1788. {
  1789. KillToStartOfLine ();
  1790. return true;
  1791. }
  1792. );
  1793. AddCommand (
  1794. Command.Paste,
  1795. () =>
  1796. {
  1797. ProcessPaste ();
  1798. return true;
  1799. }
  1800. );
  1801. AddCommand (
  1802. Command.ToggleExtend,
  1803. () =>
  1804. {
  1805. ToggleSelecting ();
  1806. return true;
  1807. }
  1808. );
  1809. AddCommand (
  1810. Command.Copy,
  1811. () =>
  1812. {
  1813. ProcessCopy ();
  1814. return true;
  1815. }
  1816. );
  1817. AddCommand (
  1818. Command.Cut,
  1819. () =>
  1820. {
  1821. ProcessCut ();
  1822. return true;
  1823. }
  1824. );
  1825. AddCommand (
  1826. Command.WordLeft,
  1827. () =>
  1828. {
  1829. ProcessMoveWordBackward ();
  1830. return true;
  1831. }
  1832. );
  1833. AddCommand (
  1834. Command.WordLeftExtend,
  1835. () =>
  1836. {
  1837. ProcessMoveWordBackwardExtend ();
  1838. return true;
  1839. }
  1840. );
  1841. AddCommand (
  1842. Command.WordRight,
  1843. () =>
  1844. {
  1845. ProcessMoveWordForward ();
  1846. return true;
  1847. }
  1848. );
  1849. AddCommand (
  1850. Command.WordRightExtend,
  1851. () =>
  1852. {
  1853. ProcessMoveWordForwardExtend ();
  1854. return true;
  1855. }
  1856. );
  1857. AddCommand (
  1858. Command.KillWordForwards,
  1859. () =>
  1860. {
  1861. ProcessKillWordForward ();
  1862. return true;
  1863. }
  1864. );
  1865. AddCommand (
  1866. Command.KillWordBackwards,
  1867. () =>
  1868. {
  1869. ProcessKillWordBackward ();
  1870. return true;
  1871. }
  1872. );
  1873. AddCommand (Command.NewLine, () => ProcessReturn ());
  1874. AddCommand (
  1875. Command.BottomEnd,
  1876. () =>
  1877. {
  1878. MoveBottomEnd ();
  1879. return true;
  1880. }
  1881. );
  1882. AddCommand (
  1883. Command.BottomEndExtend,
  1884. () =>
  1885. {
  1886. MoveBottomEndExtend ();
  1887. return true;
  1888. }
  1889. );
  1890. AddCommand (
  1891. Command.TopHome,
  1892. () =>
  1893. {
  1894. MoveTopHome ();
  1895. return true;
  1896. }
  1897. );
  1898. AddCommand (
  1899. Command.TopHomeExtend,
  1900. () =>
  1901. {
  1902. MoveTopHomeExtend ();
  1903. return true;
  1904. }
  1905. );
  1906. AddCommand (
  1907. Command.SelectAll,
  1908. () =>
  1909. {
  1910. ProcessSelectAll ();
  1911. return true;
  1912. }
  1913. );
  1914. AddCommand (
  1915. Command.ToggleOverwrite,
  1916. () =>
  1917. {
  1918. ProcessSetOverwrite ();
  1919. return true;
  1920. }
  1921. );
  1922. AddCommand (
  1923. Command.EnableOverwrite,
  1924. () =>
  1925. {
  1926. SetOverwrite (true);
  1927. return true;
  1928. }
  1929. );
  1930. AddCommand (
  1931. Command.DisableOverwrite,
  1932. () =>
  1933. {
  1934. SetOverwrite (false);
  1935. return true;
  1936. }
  1937. );
  1938. AddCommand (Command.Tab, () => ProcessTab ());
  1939. AddCommand (Command.BackTab, () => ProcessBackTab ());
  1940. AddCommand (Command.NextView, () => ProcessMoveNextView ());
  1941. AddCommand (Command.PreviousView, () => ProcessMovePreviousView ());
  1942. AddCommand (
  1943. Command.Undo,
  1944. () =>
  1945. {
  1946. Undo ();
  1947. return true;
  1948. }
  1949. );
  1950. AddCommand (
  1951. Command.Redo,
  1952. () =>
  1953. {
  1954. Redo ();
  1955. return true;
  1956. }
  1957. );
  1958. AddCommand (
  1959. Command.DeleteAll,
  1960. () =>
  1961. {
  1962. DeleteAll ();
  1963. return true;
  1964. }
  1965. );
  1966. AddCommand (
  1967. Command.ShowContextMenu,
  1968. () =>
  1969. {
  1970. ContextMenu!.Position = new Point (
  1971. CursorPosition.X - _leftColumn + 2,
  1972. CursorPosition.Y - _topRow + 2
  1973. );
  1974. ShowContextMenu ();
  1975. return true;
  1976. }
  1977. );
  1978. // Default keybindings for this view
  1979. KeyBindings.Add (Key.PageDown, Command.PageDown);
  1980. KeyBindings.Add (Key.V.WithCtrl, Command.PageDown);
  1981. KeyBindings.Add (Key.PageDown.WithShift, Command.PageDownExtend);
  1982. KeyBindings.Add (Key.PageUp, Command.PageUp);
  1983. KeyBindings.Add (Key.V.WithAlt, Command.PageUp);
  1984. KeyBindings.Add (Key.PageUp.WithShift, Command.PageUpExtend);
  1985. KeyBindings.Add (Key.N.WithCtrl, Command.LineDown);
  1986. KeyBindings.Add (Key.CursorDown, Command.LineDown);
  1987. KeyBindings.Add (Key.CursorDown.WithShift, Command.LineDownExtend);
  1988. KeyBindings.Add (Key.P.WithCtrl, Command.LineUp);
  1989. KeyBindings.Add (Key.CursorUp, Command.LineUp);
  1990. KeyBindings.Add (Key.CursorUp.WithShift, Command.LineUpExtend);
  1991. KeyBindings.Add (Key.F.WithCtrl, Command.Right);
  1992. KeyBindings.Add (Key.CursorRight, Command.Right);
  1993. KeyBindings.Add (Key.CursorRight.WithShift, Command.RightExtend);
  1994. KeyBindings.Add (Key.B.WithCtrl, Command.Left);
  1995. KeyBindings.Add (Key.CursorLeft, Command.Left);
  1996. KeyBindings.Add (Key.CursorLeft.WithShift, Command.LeftExtend);
  1997. KeyBindings.Add (Key.Backspace, Command.DeleteCharLeft);
  1998. KeyBindings.Add (Key.Home, Command.StartOfLine);
  1999. KeyBindings.Add (Key.A.WithCtrl, Command.StartOfLine);
  2000. KeyBindings.Add (Key.Home.WithShift, Command.StartOfLineExtend);
  2001. KeyBindings.Add (Key.Delete, Command.DeleteCharRight);
  2002. KeyBindings.Add (Key.D.WithCtrl, Command.DeleteCharRight);
  2003. KeyBindings.Add (Key.End, Command.EndOfLine);
  2004. KeyBindings.Add (Key.E.WithCtrl, Command.EndOfLine);
  2005. KeyBindings.Add (Key.End.WithShift, Command.EndOfLineExtend);
  2006. KeyBindings.Add (Key.K.WithCtrl, Command.CutToEndLine); // kill-to-end
  2007. KeyBindings.Add (Key.Delete.WithCtrl.WithShift, Command.CutToEndLine); // kill-to-end
  2008. KeyBindings.Add (Key.K.WithAlt, Command.CutToStartLine); // kill-to-start
  2009. KeyBindings.Add (Key.Backspace.WithCtrl.WithShift, Command.CutToStartLine); // kill-to-start
  2010. KeyBindings.Add (Key.Y.WithCtrl, Command.Paste); // Control-y, yank
  2011. KeyBindings.Add (Key.Space.WithCtrl, Command.ToggleExtend);
  2012. KeyBindings.Add (Key.C.WithAlt, Command.Copy);
  2013. KeyBindings.Add (Key.C.WithCtrl, Command.Copy);
  2014. KeyBindings.Add (Key.W.WithAlt, Command.Cut);
  2015. KeyBindings.Add (Key.W.WithCtrl, Command.Cut);
  2016. KeyBindings.Add (Key.X.WithCtrl, Command.Cut);
  2017. KeyBindings.Add (Key.CursorLeft.WithCtrl, Command.WordLeft);
  2018. KeyBindings.Add (Key.B.WithAlt, Command.WordLeft);
  2019. KeyBindings.Add (Key.CursorLeft.WithCtrl.WithShift, Command.WordLeftExtend);
  2020. KeyBindings.Add (Key.CursorRight.WithCtrl, Command.WordRight);
  2021. KeyBindings.Add (Key.F.WithAlt, Command.WordRight);
  2022. KeyBindings.Add (Key.CursorRight.WithCtrl.WithShift, Command.WordRightExtend);
  2023. KeyBindings.Add (Key.Delete.WithCtrl, Command.KillWordForwards); // kill-word-forwards
  2024. KeyBindings.Add (Key.Backspace.WithCtrl, Command.KillWordBackwards); // kill-word-backwards
  2025. // BUGBUG: If AllowsReturn is false, Key.Enter should not be bound (so that Toplevel can cause Command.Accept).
  2026. KeyBindings.Add (Key.Enter, Command.NewLine);
  2027. KeyBindings.Add (Key.End.WithCtrl, Command.BottomEnd);
  2028. KeyBindings.Add (Key.End.WithCtrl.WithShift, Command.BottomEndExtend);
  2029. KeyBindings.Add (Key.Home.WithCtrl, Command.TopHome);
  2030. KeyBindings.Add (Key.Home.WithCtrl.WithShift, Command.TopHomeExtend);
  2031. KeyBindings.Add (Key.T.WithCtrl, Command.SelectAll);
  2032. KeyBindings.Add (Key.InsertChar, Command.ToggleOverwrite);
  2033. KeyBindings.Add (Key.Tab, Command.Tab);
  2034. KeyBindings.Add (Key.Tab.WithShift, Command.BackTab);
  2035. KeyBindings.Add (Key.Tab.WithCtrl, Command.NextView);
  2036. KeyBindings.Add (Application.AlternateForwardKey, Command.NextView);
  2037. KeyBindings.Add (Key.Tab.WithCtrl.WithShift, Command.PreviousView);
  2038. KeyBindings.Add (Application.AlternateBackwardKey, Command.PreviousView);
  2039. KeyBindings.Add (Key.Z.WithCtrl, Command.Undo);
  2040. KeyBindings.Add (Key.R.WithCtrl, Command.Redo);
  2041. KeyBindings.Add (Key.G.WithCtrl, Command.DeleteAll);
  2042. KeyBindings.Add (Key.D.WithCtrl.WithShift, Command.DeleteAll);
  2043. _currentCulture = Thread.CurrentThread.CurrentUICulture;
  2044. ContextMenu = new ContextMenu { MenuItems = BuildContextMenuBarItem () };
  2045. ContextMenu.KeyChanged += ContextMenu_KeyChanged!;
  2046. KeyBindings.Add ((KeyCode)ContextMenu.Key, KeyBindingScope.HotKey, Command.ShowContextMenu);
  2047. }
  2048. /// <summary>
  2049. /// Gets or sets a value indicating whether pressing ENTER in a <see cref="TextView"/> creates a new line of text
  2050. /// in the view or activates the default button for the Toplevel.
  2051. /// </summary>
  2052. public bool AllowsReturn
  2053. {
  2054. get => _allowsReturn;
  2055. set
  2056. {
  2057. _allowsReturn = value;
  2058. if (_allowsReturn && !_multiline)
  2059. {
  2060. Multiline = true;
  2061. }
  2062. if (!_allowsReturn && _multiline)
  2063. {
  2064. Multiline = false;
  2065. AllowsTab = false;
  2066. }
  2067. SetNeedsDisplay ();
  2068. }
  2069. }
  2070. /// <summary>
  2071. /// Gets or sets whether the <see cref="TextView"/> inserts a tab character into the text or ignores tab input. If
  2072. /// set to `false` and the user presses the tab key (or shift-tab) the focus will move to the next view (or previous
  2073. /// with shift-tab). The default is `true`; if the user presses the tab key, a tab character will be inserted into the
  2074. /// text.
  2075. /// </summary>
  2076. public bool AllowsTab
  2077. {
  2078. get => _allowsTab;
  2079. set
  2080. {
  2081. _allowsTab = value;
  2082. if (_allowsTab && _tabWidth == 0)
  2083. {
  2084. _tabWidth = 4;
  2085. }
  2086. if (_allowsTab && !_multiline)
  2087. {
  2088. Multiline = true;
  2089. }
  2090. if (!_allowsTab && _tabWidth > 0)
  2091. {
  2092. _tabWidth = 0;
  2093. }
  2094. SetNeedsDisplay ();
  2095. }
  2096. }
  2097. /// <summary>
  2098. /// Provides autocomplete context menu based on suggestions at the current cursor position. Configure
  2099. /// <see cref="IAutocomplete.SuggestionGenerator"/> to enable this feature
  2100. /// </summary>
  2101. public IAutocomplete Autocomplete { get; protected set; } = new TextViewAutocomplete ();
  2102. /// <summary>
  2103. /// The bottom offset needed to use a horizontal scrollbar or for another reason. This is only needed with the
  2104. /// keyboard navigation.
  2105. /// </summary>
  2106. public int BottomOffset
  2107. {
  2108. get => _bottomOffset;
  2109. set
  2110. {
  2111. if (CurrentRow == Lines - 1 && _bottomOffset > 0 && value == 0)
  2112. {
  2113. _topRow = Math.Max (_topRow - _bottomOffset, 0);
  2114. }
  2115. _bottomOffset = value;
  2116. Adjust ();
  2117. }
  2118. }
  2119. /// <summary>Get the <see cref="ContextMenu"/> for this view.</summary>
  2120. public ContextMenu? ContextMenu { get; }
  2121. /// <summary>Gets the cursor column.</summary>
  2122. /// <value>The cursor column.</value>
  2123. public int CurrentColumn { get; private set; }
  2124. /// <summary>Gets the current cursor row.</summary>
  2125. public int CurrentRow { get; private set; }
  2126. /// <summary>Sets or gets the current cursor position.</summary>
  2127. public Point CursorPosition
  2128. {
  2129. get => new (CurrentColumn, CurrentRow);
  2130. set
  2131. {
  2132. List<RuneCell> line = _model.GetLine (Math.Max (Math.Min (value.Y, _model.Count - 1), 0));
  2133. CurrentColumn = value.X < 0 ? 0 :
  2134. value.X > line.Count ? line.Count : value.X;
  2135. CurrentRow = value.Y < 0 ? 0 :
  2136. value.Y > _model.Count - 1 ? Math.Max (_model.Count - 1, 0) : value.Y;
  2137. SetNeedsDisplay ();
  2138. Adjust ();
  2139. }
  2140. }
  2141. /// <summary>Get / Set the wished cursor when the field is focused</summary>
  2142. public CursorVisibility DesiredCursorVisibility
  2143. {
  2144. get => _desiredCursorVisibility;
  2145. set
  2146. {
  2147. if (HasFocus)
  2148. {
  2149. Application.Driver.SetCursorVisibility (value);
  2150. }
  2151. _desiredCursorVisibility = value;
  2152. SetNeedsDisplay ();
  2153. }
  2154. }
  2155. /// <summary>
  2156. /// Indicates whatever the text has history changes or not. <see langword="true"/> if the text has history changes
  2157. /// <see langword="false"/> otherwise.
  2158. /// </summary>
  2159. public bool HasHistoryChanges => _historyText.HasHistoryChanges;
  2160. /// <summary>
  2161. /// If <see langword="true"/> and the current <see cref="RuneCell.ColorScheme"/> is null will inherit from the
  2162. /// previous, otherwise if <see langword="false"/> (default) do nothing. If the text is load with
  2163. /// <see cref="Load(List{RuneCell})"/> this property is automatically sets to <see langword="true"/>.
  2164. /// </summary>
  2165. public bool InheritsPreviousColorScheme { get; set; }
  2166. /// <summary>
  2167. /// Indicates whatever the text was changed or not. <see langword="true"/> if the text was changed
  2168. /// <see langword="false"/> otherwise.
  2169. /// </summary>
  2170. public bool IsDirty
  2171. {
  2172. get => _historyText.IsDirty (Text);
  2173. set => _historyText.Clear (Text);
  2174. }
  2175. /// <summary>Gets or sets the left column.</summary>
  2176. public int LeftColumn
  2177. {
  2178. get => _leftColumn;
  2179. set
  2180. {
  2181. if (value > 0 && _wordWrap)
  2182. {
  2183. return;
  2184. }
  2185. _leftColumn = Math.Max (Math.Min (value, Maxlength - 1), 0);
  2186. }
  2187. }
  2188. /// <summary>Gets the number of lines.</summary>
  2189. public int Lines => _model.Count;
  2190. /// <summary>Gets the maximum visible length line.</summary>
  2191. public int Maxlength => _model.GetMaxVisibleLine (_topRow, _topRow + Frame.Height, TabWidth);
  2192. /// <summary>Gets or sets a value indicating whether this <see cref="TextView"/> is a multiline text view.</summary>
  2193. public bool Multiline
  2194. {
  2195. get => _multiline;
  2196. set
  2197. {
  2198. _multiline = value;
  2199. if (_multiline && !_allowsTab)
  2200. {
  2201. AllowsTab = true;
  2202. }
  2203. if (_multiline && !_allowsReturn)
  2204. {
  2205. AllowsReturn = true;
  2206. }
  2207. if (!_multiline)
  2208. {
  2209. AllowsReturn = false;
  2210. AllowsTab = false;
  2211. WordWrap = false;
  2212. CurrentColumn = 0;
  2213. CurrentRow = 0;
  2214. _savedHeight = Height;
  2215. //var prevLayoutStyle = LayoutStyle;
  2216. //if (LayoutStyle == LayoutStyle.Computed) {
  2217. // LayoutStyle = LayoutStyle.Absolute;
  2218. //}
  2219. Height = 1;
  2220. //LayoutStyle = prevLayoutStyle;
  2221. if (!IsInitialized)
  2222. {
  2223. _model.LoadString (Text);
  2224. }
  2225. SetNeedsDisplay ();
  2226. }
  2227. else if (_multiline && _savedHeight is { })
  2228. {
  2229. //var lyout = LayoutStyle;
  2230. //if (LayoutStyle == LayoutStyle.Computed) {
  2231. // LayoutStyle = LayoutStyle.Absolute;
  2232. //}
  2233. Height = _savedHeight;
  2234. //LayoutStyle = lyout;
  2235. SetNeedsDisplay ();
  2236. }
  2237. }
  2238. }
  2239. /// <summary>Gets or sets whether the <see cref="TextView"/> is in read-only mode or not</summary>
  2240. /// <value>Boolean value(Default false)</value>
  2241. public bool ReadOnly
  2242. {
  2243. get => _isReadOnly;
  2244. set
  2245. {
  2246. if (value != _isReadOnly)
  2247. {
  2248. _isReadOnly = value;
  2249. SetNeedsDisplay ();
  2250. Adjust ();
  2251. }
  2252. }
  2253. }
  2254. /// <summary>
  2255. /// The right offset needed to use a vertical scrollbar or for another reason. This is only needed with the
  2256. /// keyboard navigation.
  2257. /// </summary>
  2258. public int RightOffset
  2259. {
  2260. get => _rightOffset;
  2261. set
  2262. {
  2263. if (!_wordWrap && CurrentColumn == GetCurrentLine ().Count && _rightOffset > 0 && value == 0)
  2264. {
  2265. _leftColumn = Math.Max (_leftColumn - _rightOffset, 0);
  2266. }
  2267. _rightOffset = value;
  2268. Adjust ();
  2269. }
  2270. }
  2271. /// <summary>Length of the selected text.</summary>
  2272. public int SelectedLength => GetSelectedLength ();
  2273. /// <summary>The selected text.</summary>
  2274. public string SelectedText
  2275. {
  2276. get
  2277. {
  2278. if (!Selecting || (_model.Count == 1 && _model.GetLine (0).Count == 0))
  2279. {
  2280. return string.Empty;
  2281. }
  2282. return GetSelectedRegion ();
  2283. }
  2284. }
  2285. /// <summary>Get or sets the selecting.</summary>
  2286. public bool Selecting { get; set; }
  2287. /// <summary>Start column position of the selected text.</summary>
  2288. public int SelectionStartColumn
  2289. {
  2290. get => _selectionStartColumn;
  2291. set
  2292. {
  2293. List<RuneCell> line = _model.GetLine (_selectionStartRow);
  2294. _selectionStartColumn = value < 0 ? 0 :
  2295. value > line.Count ? line.Count : value;
  2296. Selecting = true;
  2297. SetNeedsDisplay ();
  2298. Adjust ();
  2299. }
  2300. }
  2301. /// <summary>Start row position of the selected text.</summary>
  2302. public int SelectionStartRow
  2303. {
  2304. get => _selectionStartRow;
  2305. set
  2306. {
  2307. _selectionStartRow = value < 0 ? 0 :
  2308. value > _model.Count - 1 ? Math.Max (_model.Count - 1, 0) : value;
  2309. Selecting = true;
  2310. SetNeedsDisplay ();
  2311. Adjust ();
  2312. }
  2313. }
  2314. /// <summary>Gets or sets a value indicating the number of whitespace when pressing the TAB key.</summary>
  2315. public int TabWidth
  2316. {
  2317. get => _tabWidth;
  2318. set
  2319. {
  2320. _tabWidth = Math.Max (value, 0);
  2321. if (_tabWidth > 0 && !AllowsTab)
  2322. {
  2323. AllowsTab = true;
  2324. }
  2325. SetNeedsDisplay ();
  2326. }
  2327. }
  2328. /// <summary>Sets or gets the text in the <see cref="TextView"/>.</summary>
  2329. /// <remarks>
  2330. /// The <see cref="View.TextChanged"/> event is fired whenever this property is set. Note, however, that Text is not
  2331. /// set by <see cref="TextView"/> as the user types.
  2332. /// </remarks>
  2333. public override string Text
  2334. {
  2335. get
  2336. {
  2337. if (_wordWrap)
  2338. {
  2339. return _wrapManager!.Model.ToString ();
  2340. }
  2341. return _model.ToString ();
  2342. }
  2343. set
  2344. {
  2345. var old = Text;
  2346. ResetPosition ();
  2347. _model.LoadString (value);
  2348. if (_wordWrap)
  2349. {
  2350. _wrapManager = new WordWrapManager (_model);
  2351. _model = _wrapManager.WrapModel (_frameWidth, out _, out _, out _, out _);
  2352. }
  2353. OnTextChanged (old,Text);
  2354. SetNeedsDisplay ();
  2355. _historyText.Clear (Text);
  2356. }
  2357. }
  2358. /// <summary>Gets or sets the top row.</summary>
  2359. public int TopRow
  2360. {
  2361. get => _topRow;
  2362. set => _topRow = Math.Max (Math.Min (value, Lines - 1), 0);
  2363. }
  2364. /// <summary>
  2365. /// Tracks whether the text view should be considered "used", that is, that the user has moved in the entry, so
  2366. /// new input should be appended at the cursor position, rather than clearing the entry
  2367. /// </summary>
  2368. public bool Used { get; set; }
  2369. /// <summary>Allows word wrap the to fit the available container width.</summary>
  2370. public bool WordWrap
  2371. {
  2372. get => _wordWrap;
  2373. set
  2374. {
  2375. if (value == _wordWrap)
  2376. {
  2377. return;
  2378. }
  2379. if (value && !_multiline)
  2380. {
  2381. return;
  2382. }
  2383. _wordWrap = value;
  2384. ResetPosition ();
  2385. if (_wordWrap)
  2386. {
  2387. _wrapManager = new WordWrapManager (_model);
  2388. _model = _wrapManager.WrapModel (_frameWidth, out _, out _, out _, out _);
  2389. }
  2390. else if (!_wordWrap && _wrapManager is { })
  2391. {
  2392. _model = _wrapManager.Model;
  2393. }
  2394. SetNeedsDisplay ();
  2395. }
  2396. }
  2397. private int _frameWidth => Math.Max (Frame.Width - (RightOffset != 0 ? 2 : 1), 0);
  2398. /// <summary>Allows clearing the <see cref="HistoryText.HistoryTextItem"/> items updating the original text.</summary>
  2399. public void ClearHistoryChanges () { _historyText?.Clear (Text); }
  2400. /// <summary>Closes the contents of the stream into the <see cref="TextView"/>.</summary>
  2401. /// <returns><c>true</c>, if stream was closed, <c>false</c> otherwise.</returns>
  2402. public bool CloseFile ()
  2403. {
  2404. SetWrapModel ();
  2405. bool res = _model.CloseFile ();
  2406. ResetPosition ();
  2407. SetNeedsDisplay ();
  2408. UpdateWrapModel ();
  2409. return res;
  2410. }
  2411. /// <summary>Raised when the contents of the <see cref="TextView"/> are changed.</summary>
  2412. /// <remarks>
  2413. /// Unlike the <see cref="View.TextChanged"/> event, this event is raised whenever the user types or otherwise changes
  2414. /// the contents of the <see cref="TextView"/>.
  2415. /// </remarks>
  2416. public event EventHandler<ContentsChangedEventArgs>? ContentsChanged;
  2417. /// <summary>Copy the selected text to the clipboard contents.</summary>
  2418. public void Copy ()
  2419. {
  2420. SetWrapModel ();
  2421. if (Selecting)
  2422. {
  2423. SetClipboard (GetRegion ());
  2424. _copyWithoutSelection = false;
  2425. }
  2426. else
  2427. {
  2428. List<RuneCell> currentLine = GetCurrentLine ();
  2429. SetClipboard (TextModel.ToString (currentLine));
  2430. _copyWithoutSelection = true;
  2431. }
  2432. UpdateWrapModel ();
  2433. DoNeededAction ();
  2434. }
  2435. /// <summary>Cut the selected text to the clipboard contents.</summary>
  2436. public void Cut ()
  2437. {
  2438. SetWrapModel ();
  2439. SetClipboard (GetRegion ());
  2440. if (!_isReadOnly)
  2441. {
  2442. ClearRegion ();
  2443. _historyText.Add (
  2444. new List<List<RuneCell>> { new (GetCurrentLine ()) },
  2445. CursorPosition,
  2446. HistoryText.LineStatus.Replaced
  2447. );
  2448. }
  2449. UpdateWrapModel ();
  2450. Selecting = false;
  2451. DoNeededAction ();
  2452. OnContentsChanged ();
  2453. }
  2454. /// <summary>Deletes all text.</summary>
  2455. public void DeleteAll ()
  2456. {
  2457. if (Lines == 0)
  2458. {
  2459. return;
  2460. }
  2461. _selectionStartColumn = 0;
  2462. _selectionStartRow = 0;
  2463. MoveBottomEndExtend ();
  2464. DeleteCharLeft ();
  2465. SetNeedsDisplay ();
  2466. }
  2467. /// <summary>Deletes all the selected or a single character at left from the position of the cursor.</summary>
  2468. public void DeleteCharLeft ()
  2469. {
  2470. if (_isReadOnly)
  2471. {
  2472. return;
  2473. }
  2474. SetWrapModel ();
  2475. if (Selecting)
  2476. {
  2477. _historyText.Add (new List<List<RuneCell>> { new (GetCurrentLine ()) }, CursorPosition);
  2478. ClearSelectedRegion ();
  2479. List<RuneCell> currentLine = GetCurrentLine ();
  2480. _historyText.Add (
  2481. new List<List<RuneCell>> { new (currentLine) },
  2482. CursorPosition,
  2483. HistoryText.LineStatus.Replaced
  2484. );
  2485. UpdateWrapModel ();
  2486. OnContentsChanged ();
  2487. return;
  2488. }
  2489. if (DeleteTextBackwards ())
  2490. {
  2491. UpdateWrapModel ();
  2492. OnContentsChanged ();
  2493. return;
  2494. }
  2495. UpdateWrapModel ();
  2496. DoNeededAction ();
  2497. OnContentsChanged ();
  2498. }
  2499. /// <summary>Deletes all the selected or a single character at right from the position of the cursor.</summary>
  2500. public void DeleteCharRight ()
  2501. {
  2502. if (_isReadOnly)
  2503. {
  2504. return;
  2505. }
  2506. SetWrapModel ();
  2507. if (Selecting)
  2508. {
  2509. _historyText.Add (new List<List<RuneCell>> { new (GetCurrentLine ()) }, CursorPosition);
  2510. ClearSelectedRegion ();
  2511. List<RuneCell> currentLine = GetCurrentLine ();
  2512. _historyText.Add (
  2513. new List<List<RuneCell>> { new (currentLine) },
  2514. CursorPosition,
  2515. HistoryText.LineStatus.Replaced
  2516. );
  2517. UpdateWrapModel ();
  2518. OnContentsChanged ();
  2519. return;
  2520. }
  2521. if (DeleteTextForwards ())
  2522. {
  2523. UpdateWrapModel ();
  2524. OnContentsChanged ();
  2525. return;
  2526. }
  2527. UpdateWrapModel ();
  2528. DoNeededAction ();
  2529. OnContentsChanged ();
  2530. }
  2531. /// <summary>Invoked when the normal color is drawn.</summary>
  2532. public event EventHandler<RuneCellEventArgs>? DrawNormalColor;
  2533. /// <summary>Invoked when the ready only color is drawn.</summary>
  2534. public event EventHandler<RuneCellEventArgs>? DrawReadOnlyColor;
  2535. /// <summary>Invoked when the selection color is drawn.</summary>
  2536. public event EventHandler<RuneCellEventArgs>? DrawSelectionColor;
  2537. /// <summary>
  2538. /// Invoked when the used color is drawn. The Used Color is used to indicate if the <see cref="Key.InsertChar"/>
  2539. /// was pressed and enabled.
  2540. /// </summary>
  2541. public event EventHandler<RuneCellEventArgs>? DrawUsedColor;
  2542. /// <summary>Find the next text based on the match case with the option to replace it.</summary>
  2543. /// <param name="textToFind">The text to find.</param>
  2544. /// <param name="gaveFullTurn"><c>true</c>If all the text was forward searched.<c>false</c>otherwise.</param>
  2545. /// <param name="matchCase">The match case setting.</param>
  2546. /// <param name="matchWholeWord">The match whole word setting.</param>
  2547. /// <param name="textToReplace">The text to replace.</param>
  2548. /// <param name="replace"><c>true</c>If is replacing.<c>false</c>otherwise.</param>
  2549. /// <returns><c>true</c>If the text was found.<c>false</c>otherwise.</returns>
  2550. public bool FindNextText (
  2551. string textToFind,
  2552. out bool gaveFullTurn,
  2553. bool matchCase = false,
  2554. bool matchWholeWord = false,
  2555. string? textToReplace = null,
  2556. bool replace = false
  2557. )
  2558. {
  2559. if (_model.Count == 0)
  2560. {
  2561. gaveFullTurn = false;
  2562. return false;
  2563. }
  2564. SetWrapModel ();
  2565. ResetContinuousFind ();
  2566. (Point current, bool found) foundPos =
  2567. _model.FindNextText (textToFind, out gaveFullTurn, matchCase, matchWholeWord);
  2568. return SetFoundText (textToFind, foundPos, textToReplace, replace);
  2569. }
  2570. /// <summary>Find the previous text based on the match case with the option to replace it.</summary>
  2571. /// <param name="textToFind">The text to find.</param>
  2572. /// <param name="gaveFullTurn"><c>true</c>If all the text was backward searched.<c>false</c>otherwise.</param>
  2573. /// <param name="matchCase">The match case setting.</param>
  2574. /// <param name="matchWholeWord">The match whole word setting.</param>
  2575. /// <param name="textToReplace">The text to replace.</param>
  2576. /// <param name="replace"><c>true</c>If the text was found.<c>false</c>otherwise.</param>
  2577. /// <returns><c>true</c>If the text was found.<c>false</c>otherwise.</returns>
  2578. public bool FindPreviousText (
  2579. string textToFind,
  2580. out bool gaveFullTurn,
  2581. bool matchCase = false,
  2582. bool matchWholeWord = false,
  2583. string? textToReplace = null,
  2584. bool replace = false
  2585. )
  2586. {
  2587. if (_model.Count == 0)
  2588. {
  2589. gaveFullTurn = false;
  2590. return false;
  2591. }
  2592. SetWrapModel ();
  2593. ResetContinuousFind ();
  2594. (Point current, bool found) foundPos =
  2595. _model.FindPreviousText (textToFind, out gaveFullTurn, matchCase, matchWholeWord);
  2596. return SetFoundText (textToFind, foundPos, textToReplace, replace);
  2597. }
  2598. /// <summary>Reset the flag to stop continuous find.</summary>
  2599. public void FindTextChanged () { _continuousFind = false; }
  2600. /// <summary>Gets all lines of characters.</summary>
  2601. /// <returns></returns>
  2602. public List<List<RuneCell>> GetAllLines () { return _model.GetAllLines (); }
  2603. /// <summary>
  2604. /// Returns the characters on the current line (where the cursor is positioned). Use <see cref="CurrentColumn"/>
  2605. /// to determine the position of the cursor within that line
  2606. /// </summary>
  2607. /// <returns></returns>
  2608. public List<RuneCell> GetCurrentLine () { return _model.GetLine (CurrentRow); }
  2609. /// <summary>Returns the characters on the <paramref name="line"/>.</summary>
  2610. /// <param name="line">The intended line.</param>
  2611. /// <returns></returns>
  2612. public List<RuneCell> GetLine (int line) { return _model.GetLine (line); }
  2613. /// <inheritdoc/>
  2614. public override Attribute GetNormalColor ()
  2615. {
  2616. ColorScheme? cs = ColorScheme;
  2617. if (ColorScheme is null)
  2618. {
  2619. cs = new ColorScheme ();
  2620. }
  2621. return Enabled ? cs.Focus : cs.Disabled;
  2622. }
  2623. /// <summary>
  2624. /// Inserts the given <paramref name="toAdd"/> text at the current cursor position exactly as if the user had just
  2625. /// typed it
  2626. /// </summary>
  2627. /// <param name="toAdd">Text to add</param>
  2628. public void InsertText (string toAdd)
  2629. {
  2630. foreach (char ch in toAdd)
  2631. {
  2632. Key key;
  2633. try
  2634. {
  2635. key = new Key (ch);
  2636. }
  2637. catch (Exception)
  2638. {
  2639. throw new ArgumentException (
  2640. $"Cannot insert character '{ch}' because it does not map to a Key"
  2641. );
  2642. }
  2643. InsertText (key);
  2644. if (NeedsDisplay)
  2645. {
  2646. Adjust ();
  2647. }
  2648. else
  2649. {
  2650. PositionCursor ();
  2651. }
  2652. }
  2653. }
  2654. /// <summary>Loads the contents of the file into the <see cref="TextView"/>.</summary>
  2655. /// <returns><c>true</c>, if file was loaded, <c>false</c> otherwise.</returns>
  2656. /// <param name="path">Path to the file to load.</param>
  2657. public bool Load (string path)
  2658. {
  2659. SetWrapModel ();
  2660. bool res;
  2661. try
  2662. {
  2663. SetWrapModel ();
  2664. res = _model.LoadFile (path);
  2665. _historyText.Clear (Text);
  2666. ResetPosition ();
  2667. }
  2668. finally
  2669. {
  2670. UpdateWrapModel ();
  2671. SetNeedsDisplay ();
  2672. Adjust ();
  2673. }
  2674. UpdateWrapModel ();
  2675. return res;
  2676. }
  2677. /// <summary>Loads the contents of the stream into the <see cref="TextView"/>.</summary>
  2678. /// <returns><c>true</c>, if stream was loaded, <c>false</c> otherwise.</returns>
  2679. /// <param name="stream">Stream to load the contents from.</param>
  2680. public void Load (Stream stream)
  2681. {
  2682. SetWrapModel ();
  2683. _model.LoadStream (stream);
  2684. _historyText.Clear (Text);
  2685. ResetPosition ();
  2686. SetNeedsDisplay ();
  2687. UpdateWrapModel ();
  2688. }
  2689. /// <summary>Loads the contents of the <see cref="RuneCell"/> list into the <see cref="TextView"/>.</summary>
  2690. /// <param name="cells">Rune cells list to load the contents from.</param>
  2691. public void Load (List<RuneCell> cells)
  2692. {
  2693. SetWrapModel ();
  2694. _model.LoadRuneCells (cells, ColorScheme);
  2695. _historyText.Clear (Text);
  2696. ResetPosition ();
  2697. SetNeedsDisplay ();
  2698. UpdateWrapModel ();
  2699. InheritsPreviousColorScheme = true;
  2700. }
  2701. /// <summary>Loads the contents of the list of <see cref="RuneCell"/> list into the <see cref="TextView"/>.</summary>
  2702. /// <param name="cellsList">List of rune cells list to load the contents from.</param>
  2703. public void Load (List<List<RuneCell>> cellsList)
  2704. {
  2705. SetWrapModel ();
  2706. InheritsPreviousColorScheme = true;
  2707. _model.LoadListRuneCells (cellsList, ColorScheme);
  2708. _historyText.Clear (Text);
  2709. ResetPosition ();
  2710. SetNeedsDisplay ();
  2711. UpdateWrapModel ();
  2712. }
  2713. /// <inheritdoc/>
  2714. protected internal override bool OnMouseEvent (MouseEvent ev)
  2715. {
  2716. if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked)
  2717. && !ev.Flags.HasFlag (MouseFlags.Button1Pressed)
  2718. && !ev.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)
  2719. && !ev.Flags.HasFlag (MouseFlags.Button1Released)
  2720. && !ev.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ButtonShift)
  2721. && !ev.Flags.HasFlag (MouseFlags.WheeledDown)
  2722. && !ev.Flags.HasFlag (MouseFlags.WheeledUp)
  2723. && !ev.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
  2724. && !ev.Flags.HasFlag (MouseFlags.Button1DoubleClicked | MouseFlags.ButtonShift)
  2725. && !ev.Flags.HasFlag (MouseFlags.Button1TripleClicked)
  2726. && !ev.Flags.HasFlag (ContextMenu!.MouseFlags))
  2727. {
  2728. return false;
  2729. }
  2730. if (!CanFocus)
  2731. {
  2732. return true;
  2733. }
  2734. if (!HasFocus)
  2735. {
  2736. SetFocus ();
  2737. }
  2738. _continuousFind = false;
  2739. // Give autocomplete first opportunity to respond to mouse clicks
  2740. if (SelectedLength == 0 && Autocomplete.OnMouseEvent (ev, true))
  2741. {
  2742. return true;
  2743. }
  2744. if (ev.Flags == MouseFlags.Button1Clicked)
  2745. {
  2746. if (_shiftSelecting && !_isButtonShift)
  2747. {
  2748. StopSelecting ();
  2749. }
  2750. ProcessMouseClick (ev, out _);
  2751. if (Used)
  2752. {
  2753. PositionCursor ();
  2754. }
  2755. else
  2756. {
  2757. SetNeedsDisplay ();
  2758. }
  2759. _lastWasKill = false;
  2760. _columnTrack = CurrentColumn;
  2761. }
  2762. else if (ev.Flags == MouseFlags.WheeledDown)
  2763. {
  2764. _lastWasKill = false;
  2765. _columnTrack = CurrentColumn;
  2766. ScrollTo (_topRow + 1);
  2767. }
  2768. else if (ev.Flags == MouseFlags.WheeledUp)
  2769. {
  2770. _lastWasKill = false;
  2771. _columnTrack = CurrentColumn;
  2772. ScrollTo (_topRow - 1);
  2773. }
  2774. else if (ev.Flags == MouseFlags.WheeledRight)
  2775. {
  2776. _lastWasKill = false;
  2777. _columnTrack = CurrentColumn;
  2778. ScrollTo (_leftColumn + 1, false);
  2779. }
  2780. else if (ev.Flags == MouseFlags.WheeledLeft)
  2781. {
  2782. _lastWasKill = false;
  2783. _columnTrack = CurrentColumn;
  2784. ScrollTo (_leftColumn - 1, false);
  2785. }
  2786. else if (ev.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  2787. {
  2788. ProcessMouseClick (ev, out List<RuneCell> line);
  2789. PositionCursor ();
  2790. if (_model.Count > 0 && _shiftSelecting && Selecting)
  2791. {
  2792. if (CurrentRow - _topRow + BottomOffset >= Frame.Height - 1 && _model.Count + BottomOffset > _topRow + CurrentRow)
  2793. {
  2794. ScrollTo (_topRow + Frame.Height);
  2795. }
  2796. else if (_topRow > 0 && CurrentRow <= _topRow)
  2797. {
  2798. ScrollTo (_topRow - Frame.Height);
  2799. }
  2800. else if (ev.Y >= Frame.Height)
  2801. {
  2802. ScrollTo (_model.Count + BottomOffset);
  2803. }
  2804. else if (ev.Y < 0 && _topRow > 0)
  2805. {
  2806. ScrollTo (0);
  2807. }
  2808. if (CurrentColumn - _leftColumn + RightOffset >= Frame.Width - 1 && line.Count + RightOffset > _leftColumn + CurrentColumn)
  2809. {
  2810. ScrollTo (_leftColumn + Frame.Width, false);
  2811. }
  2812. else if (_leftColumn > 0 && CurrentColumn <= _leftColumn)
  2813. {
  2814. ScrollTo (_leftColumn - Frame.Width, false);
  2815. }
  2816. else if (ev.X >= Frame.Width)
  2817. {
  2818. ScrollTo (line.Count + RightOffset, false);
  2819. }
  2820. else if (ev.X < 0 && _leftColumn > 0)
  2821. {
  2822. ScrollTo (0, false);
  2823. }
  2824. }
  2825. _lastWasKill = false;
  2826. _columnTrack = CurrentColumn;
  2827. }
  2828. else if (ev.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ButtonShift))
  2829. {
  2830. if (!_shiftSelecting)
  2831. {
  2832. _isButtonShift = true;
  2833. StartSelecting ();
  2834. }
  2835. ProcessMouseClick (ev, out _);
  2836. PositionCursor ();
  2837. _lastWasKill = false;
  2838. _columnTrack = CurrentColumn;
  2839. }
  2840. else if (ev.Flags.HasFlag (MouseFlags.Button1Pressed))
  2841. {
  2842. if (_shiftSelecting)
  2843. {
  2844. _clickWithSelecting = true;
  2845. StopSelecting ();
  2846. }
  2847. ProcessMouseClick (ev, out _);
  2848. PositionCursor ();
  2849. if (!Selecting)
  2850. {
  2851. StartSelecting ();
  2852. }
  2853. _lastWasKill = false;
  2854. _columnTrack = CurrentColumn;
  2855. if (Application.MouseGrabView is null)
  2856. {
  2857. Application.GrabMouse (this);
  2858. }
  2859. }
  2860. else if (ev.Flags.HasFlag (MouseFlags.Button1Released))
  2861. {
  2862. Application.UngrabMouse ();
  2863. }
  2864. else if (ev.Flags.HasFlag (MouseFlags.Button1DoubleClicked))
  2865. {
  2866. if (ev.Flags.HasFlag (MouseFlags.ButtonShift))
  2867. {
  2868. if (!Selecting)
  2869. {
  2870. StartSelecting ();
  2871. }
  2872. }
  2873. else if (Selecting)
  2874. {
  2875. StopSelecting ();
  2876. }
  2877. ProcessMouseClick (ev, out List<RuneCell> line);
  2878. (int col, int row)? newPos;
  2879. if (CurrentColumn == line.Count
  2880. || (CurrentColumn > 0 && (line [CurrentColumn - 1].Rune.Value != ' ' || line [CurrentColumn].Rune.Value == ' ')))
  2881. {
  2882. newPos = _model.WordBackward (CurrentColumn, CurrentRow);
  2883. if (newPos.HasValue)
  2884. {
  2885. CurrentColumn = CurrentRow == newPos.Value.row ? newPos.Value.col : 0;
  2886. }
  2887. }
  2888. if (!Selecting)
  2889. {
  2890. StartSelecting ();
  2891. }
  2892. newPos = _model.WordForward (CurrentColumn, CurrentRow);
  2893. if (newPos is { } && newPos.HasValue)
  2894. {
  2895. CurrentColumn = CurrentRow == newPos.Value.row ? newPos.Value.col : line.Count;
  2896. }
  2897. PositionCursor ();
  2898. _lastWasKill = false;
  2899. _columnTrack = CurrentColumn;
  2900. }
  2901. else if (ev.Flags.HasFlag (MouseFlags.Button1TripleClicked))
  2902. {
  2903. if (Selecting)
  2904. {
  2905. StopSelecting ();
  2906. }
  2907. ProcessMouseClick (ev, out List<RuneCell> line);
  2908. CurrentColumn = 0;
  2909. if (!Selecting)
  2910. {
  2911. StartSelecting ();
  2912. }
  2913. CurrentColumn = line.Count;
  2914. PositionCursor ();
  2915. _lastWasKill = false;
  2916. _columnTrack = CurrentColumn;
  2917. }
  2918. else if (ev.Flags == ContextMenu!.MouseFlags)
  2919. {
  2920. ContextMenu.Position = new Point (ev.X + 2, ev.Y + 2);
  2921. ShowContextMenu ();
  2922. }
  2923. return true;
  2924. }
  2925. /// <summary>Will scroll the <see cref="TextView"/> to the last line and position the cursor there.</summary>
  2926. public void MoveEnd ()
  2927. {
  2928. CurrentRow = _model.Count - 1;
  2929. List<RuneCell> line = GetCurrentLine ();
  2930. CurrentColumn = line.Count;
  2931. TrackColumn ();
  2932. PositionCursor ();
  2933. }
  2934. /// <summary>Will scroll the <see cref="TextView"/> to the first line and position the cursor there.</summary>
  2935. public void MoveHome ()
  2936. {
  2937. CurrentRow = 0;
  2938. _topRow = 0;
  2939. CurrentColumn = 0;
  2940. _leftColumn = 0;
  2941. TrackColumn ();
  2942. PositionCursor ();
  2943. SetNeedsDisplay ();
  2944. }
  2945. /// <summary>
  2946. /// Called when the contents of the TextView change. E.g. when the user types text or deletes text. Raises the
  2947. /// <see cref="ContentsChanged"/> event.
  2948. /// </summary>
  2949. public virtual void OnContentsChanged ()
  2950. {
  2951. ContentsChanged?.Invoke (this, new ContentsChangedEventArgs (CurrentRow, CurrentColumn));
  2952. ProcessInheritsPreviousColorScheme (CurrentRow, CurrentColumn);
  2953. ProcessAutocomplete ();
  2954. }
  2955. /// <inheritdoc/>
  2956. public override void OnDrawContent (Rectangle viewport)
  2957. {
  2958. _isDrawing = true;
  2959. SetNormalColor ();
  2960. (int width, int height) offB = OffSetBackground ();
  2961. int right = Frame.Width + offB.width + RightOffset;
  2962. int bottom = Frame.Height + offB.height + BottomOffset;
  2963. var row = 0;
  2964. for (int idxRow = _topRow; idxRow < _model.Count; idxRow++)
  2965. {
  2966. List<RuneCell> line = _model.GetLine (idxRow);
  2967. int lineRuneCount = line.Count;
  2968. var col = 0;
  2969. Move (0, row);
  2970. for (int idxCol = _leftColumn; idxCol < lineRuneCount; idxCol++)
  2971. {
  2972. Rune rune = idxCol >= lineRuneCount ? (Rune)' ' : line [idxCol].Rune;
  2973. int cols = rune.GetColumns ();
  2974. if (idxCol < line.Count && Selecting && PointInSelection (idxCol, idxRow))
  2975. {
  2976. OnDrawSelectionColor (line, idxCol, idxRow);
  2977. }
  2978. else if (idxCol == CurrentColumn && idxRow == CurrentRow && !Selecting && !Used && HasFocus && idxCol < lineRuneCount)
  2979. {
  2980. OnDrawUsedColor (line, idxCol, idxRow);
  2981. }
  2982. else if (ReadOnly)
  2983. {
  2984. OnDrawReadOnlyColor (line, idxCol, idxRow);
  2985. }
  2986. else
  2987. {
  2988. OnDrawNormalColor (line, idxCol, idxRow);
  2989. }
  2990. if (rune.Value == '\t')
  2991. {
  2992. cols += TabWidth + 1;
  2993. if (col + cols > right)
  2994. {
  2995. cols = right - col;
  2996. }
  2997. for (var i = 0; i < cols; i++)
  2998. {
  2999. if (col + i < right)
  3000. {
  3001. AddRune (col + i, row, (Rune)' ');
  3002. }
  3003. }
  3004. }
  3005. else
  3006. {
  3007. AddRune (col, row, rune);
  3008. }
  3009. if (!TextModel.SetCol (ref col, viewport.Right, cols))
  3010. {
  3011. break;
  3012. }
  3013. if (idxCol + 1 < lineRuneCount && col + line [idxCol + 1].Rune.GetColumns () > right)
  3014. {
  3015. break;
  3016. }
  3017. }
  3018. if (col < right)
  3019. {
  3020. SetNormalColor ();
  3021. ClearRegion (col, row, right, row + 1);
  3022. }
  3023. row++;
  3024. }
  3025. if (row < bottom)
  3026. {
  3027. SetNormalColor ();
  3028. ClearRegion (viewport.Left, row, right, bottom);
  3029. }
  3030. PositionCursor ();
  3031. _isDrawing = false;
  3032. }
  3033. /// <inheritdoc/>
  3034. public override bool OnEnter (View view)
  3035. {
  3036. //TODO: Improve it by handling read only mode of the text field
  3037. Application.Driver.SetCursorVisibility (DesiredCursorVisibility);
  3038. return base.OnEnter (view);
  3039. }
  3040. /// <inheritdoc/>
  3041. public override bool? OnInvokingKeyBindings (Key a)
  3042. {
  3043. if (!a.IsValid)
  3044. {
  3045. return false;
  3046. }
  3047. // Give autocomplete first opportunity to respond to key presses
  3048. if (SelectedLength == 0 && Autocomplete.Suggestions.Count > 0 && Autocomplete.ProcessKey (a))
  3049. {
  3050. return true;
  3051. }
  3052. return base.OnInvokingKeyBindings (a);
  3053. }
  3054. /// <inheritdoc/>
  3055. public override bool OnKeyUp (Key key)
  3056. {
  3057. if (key == Key.Space.WithCtrl)
  3058. {
  3059. return true;
  3060. }
  3061. return base.OnKeyUp (key);
  3062. }
  3063. /// <inheritdoc/>
  3064. public override bool OnLeave (View view)
  3065. {
  3066. if (Application.MouseGrabView is { } && Application.MouseGrabView == this)
  3067. {
  3068. Application.UngrabMouse ();
  3069. }
  3070. return base.OnLeave (view);
  3071. }
  3072. /// <inheritdoc/>
  3073. public override bool OnProcessKeyDown (Key a)
  3074. {
  3075. if (!CanFocus)
  3076. {
  3077. return true;
  3078. }
  3079. ResetColumnTrack ();
  3080. // Ignore control characters and other special keys
  3081. if (!a.IsKeyCodeAtoZ && (a.KeyCode < KeyCode.Space || a.KeyCode > KeyCode.CharMask))
  3082. {
  3083. return false;
  3084. }
  3085. InsertText (a);
  3086. DoNeededAction ();
  3087. return true;
  3088. }
  3089. /// <summary>Invoke the <see cref="UnwrappedCursorPosition"/> event with the unwrapped <see cref="CursorPosition"/>.</summary>
  3090. public virtual void OnUnwrappedCursorPosition (int? cRow = null, int? cCol = null)
  3091. {
  3092. int? row = cRow is null ? CurrentRow : cRow;
  3093. int? col = cCol is null ? CurrentColumn : cCol;
  3094. if (cRow is null && cCol is null && _wordWrap)
  3095. {
  3096. row = _wrapManager!.GetModelLineFromWrappedLines (CurrentRow);
  3097. col = _wrapManager.GetModelColFromWrappedLines (CurrentRow, CurrentColumn);
  3098. }
  3099. UnwrappedCursorPosition?.Invoke (this, new PointEventArgs (new Point ((int)col, (int)row)));
  3100. }
  3101. /// <summary>Paste the clipboard contents into the current selected position.</summary>
  3102. public void Paste ()
  3103. {
  3104. if (_isReadOnly)
  3105. {
  3106. return;
  3107. }
  3108. SetWrapModel ();
  3109. string? contents = Clipboard.Contents;
  3110. if (_copyWithoutSelection && contents.FirstOrDefault (x => x == '\n' || x == '\r') == 0)
  3111. {
  3112. List<RuneCell> runeList = contents is null ? new List<RuneCell> () : TextModel.ToRuneCellList (contents);
  3113. List<RuneCell> currentLine = GetCurrentLine ();
  3114. _historyText.Add (new List<List<RuneCell>> { new (currentLine) }, CursorPosition);
  3115. List<List<RuneCell>> addedLine = new () { new List<RuneCell> (currentLine), runeList };
  3116. _historyText.Add (
  3117. new List<List<RuneCell>> (addedLine),
  3118. CursorPosition,
  3119. HistoryText.LineStatus.Added
  3120. );
  3121. _model.AddLine (CurrentRow, runeList);
  3122. CurrentRow++;
  3123. _historyText.Add (
  3124. new List<List<RuneCell>> { new (GetCurrentLine ()) },
  3125. CursorPosition,
  3126. HistoryText.LineStatus.Replaced
  3127. );
  3128. SetNeedsDisplay ();
  3129. OnContentsChanged ();
  3130. }
  3131. else
  3132. {
  3133. if (Selecting)
  3134. {
  3135. ClearRegion ();
  3136. }
  3137. _copyWithoutSelection = false;
  3138. InsertAllText (contents);
  3139. if (Selecting)
  3140. {
  3141. _historyText.ReplaceLast (
  3142. new List<List<RuneCell>> { new (GetCurrentLine ()) },
  3143. CursorPosition,
  3144. HistoryText.LineStatus.Original
  3145. );
  3146. }
  3147. SetNeedsDisplay ();
  3148. }
  3149. UpdateWrapModel ();
  3150. Selecting = false;
  3151. DoNeededAction ();
  3152. }
  3153. /// <summary>Positions the cursor on the current row and column</summary>
  3154. public override void PositionCursor ()
  3155. {
  3156. ProcessAutocomplete ();
  3157. if (!CanFocus || !Enabled || Application.Driver is null)
  3158. {
  3159. return;
  3160. }
  3161. if (Selecting)
  3162. {
  3163. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  3164. //var minRow = Math.Min (Math.Max (Math.Min (selectionStartRow, currentRow) - topRow, 0), Frame.Height);
  3165. //var maxRow = Math.Min (Math.Max (Math.Max (selectionStartRow, currentRow) - topRow, 0), Frame.Height);
  3166. //SetNeedsDisplay (new (0, minRow, Frame.Width, maxRow));
  3167. SetNeedsDisplay ();
  3168. }
  3169. List<RuneCell> line = _model.GetLine (CurrentRow);
  3170. var col = 0;
  3171. if (line.Count > 0)
  3172. {
  3173. for (int idx = _leftColumn; idx < line.Count; idx++)
  3174. {
  3175. if (idx >= CurrentColumn)
  3176. {
  3177. break;
  3178. }
  3179. int cols = line [idx].Rune.GetColumns ();
  3180. if (line [idx].Rune.Value == '\t')
  3181. {
  3182. cols += TabWidth + 1;
  3183. }
  3184. if (!TextModel.SetCol (ref col, Frame.Width, cols))
  3185. {
  3186. col = CurrentColumn;
  3187. break;
  3188. }
  3189. }
  3190. }
  3191. int posX = CurrentColumn - _leftColumn;
  3192. int posY = CurrentRow - _topRow;
  3193. if (posX > -1 && col >= posX && posX < Frame.Width - RightOffset && _topRow <= CurrentRow && posY < Frame.Height - BottomOffset)
  3194. {
  3195. ResetCursorVisibility ();
  3196. Move (col, CurrentRow - _topRow);
  3197. }
  3198. else
  3199. {
  3200. SaveCursorVisibility ();
  3201. }
  3202. }
  3203. /// <summary>Redoes the latest changes.</summary>
  3204. public void Redo ()
  3205. {
  3206. if (ReadOnly)
  3207. {
  3208. return;
  3209. }
  3210. _historyText.Redo ();
  3211. }
  3212. /// <summary>Replaces all the text based on the match case.</summary>
  3213. /// <param name="textToFind">The text to find.</param>
  3214. /// <param name="matchCase">The match case setting.</param>
  3215. /// <param name="matchWholeWord">The match whole word setting.</param>
  3216. /// <param name="textToReplace">The text to replace.</param>
  3217. /// <returns><c>true</c>If the text was found.<c>false</c>otherwise.</returns>
  3218. public bool ReplaceAllText (
  3219. string textToFind,
  3220. bool matchCase = false,
  3221. bool matchWholeWord = false,
  3222. string? textToReplace = null
  3223. )
  3224. {
  3225. if (_isReadOnly || _model.Count == 0)
  3226. {
  3227. return false;
  3228. }
  3229. SetWrapModel ();
  3230. ResetContinuousFind ();
  3231. (Point current, bool found) foundPos =
  3232. _model.ReplaceAllText (textToFind, matchCase, matchWholeWord, textToReplace);
  3233. return SetFoundText (textToFind, foundPos, textToReplace, false, true);
  3234. }
  3235. /// <summary>
  3236. /// Will scroll the <see cref="TextView"/> to display the specified row at the top if <paramref name="isRow"/> is
  3237. /// true or will scroll the <see cref="TextView"/> to display the specified column at the left if
  3238. /// <paramref name="isRow"/> is false.
  3239. /// </summary>
  3240. /// <param name="idx">
  3241. /// Row that should be displayed at the top or Column that should be displayed at the left, if the value
  3242. /// is negative it will be reset to zero
  3243. /// </param>
  3244. /// <param name="isRow">If true (default) the <paramref name="idx"/> is a row, column otherwise.</param>
  3245. public void ScrollTo (int idx, bool isRow = true)
  3246. {
  3247. if (idx < 0)
  3248. {
  3249. idx = 0;
  3250. }
  3251. if (isRow)
  3252. {
  3253. _topRow = Math.Max (idx > _model.Count - 1 ? _model.Count - 1 : idx, 0);
  3254. }
  3255. else if (!_wordWrap)
  3256. {
  3257. int maxlength =
  3258. _model.GetMaxVisibleLine (_topRow, _topRow + Frame.Height + RightOffset, TabWidth);
  3259. _leftColumn = Math.Max (!_wordWrap && idx > maxlength - 1 ? maxlength - 1 : idx, 0);
  3260. }
  3261. SetNeedsDisplay ();
  3262. }
  3263. /// <summary>Select all text.</summary>
  3264. public void SelectAll ()
  3265. {
  3266. if (_model.Count == 0)
  3267. {
  3268. return;
  3269. }
  3270. StartSelecting ();
  3271. _selectionStartColumn = 0;
  3272. _selectionStartRow = 0;
  3273. CurrentColumn = _model.GetLine (_model.Count - 1).Count;
  3274. CurrentRow = _model.Count - 1;
  3275. SetNeedsDisplay ();
  3276. }
  3277. ///// <summary>Raised when the <see cref="Text"/> property of the <see cref="TextView"/> changes.</summary>
  3278. ///// <remarks>
  3279. ///// The <see cref="Text"/> property of <see cref="TextView"/> only changes when it is explicitly set, not as the
  3280. ///// user types. To be notified as the user changes the contents of the TextView see <see cref="IsDirty"/>.
  3281. ///// </remarks>
  3282. //public event EventHandler? TextChanged;
  3283. /// <summary>Undoes the latest changes.</summary>
  3284. public void Undo ()
  3285. {
  3286. if (ReadOnly)
  3287. {
  3288. return;
  3289. }
  3290. _historyText.Undo ();
  3291. }
  3292. /// <summary>Invoked with the unwrapped <see cref="CursorPosition"/>.</summary>
  3293. public event EventHandler<PointEventArgs>? UnwrappedCursorPosition;
  3294. /// <summary>
  3295. /// Sets the <see cref="View.Driver"/> to an appropriate color for rendering the given <paramref name="idxCol"/>
  3296. /// of the current <paramref name="line"/>. Override to provide custom coloring by calling
  3297. /// <see cref="ConsoleDriver.SetAttribute(Attribute)"/> Defaults to <see cref="ColorScheme.Normal"/>.
  3298. /// </summary>
  3299. /// <param name="line">The line.</param>
  3300. /// <param name="idxCol">The col index.</param>
  3301. /// <param name="idxRow">The row index.</param>
  3302. protected virtual void OnDrawNormalColor (List<RuneCell> line, int idxCol, int idxRow)
  3303. {
  3304. (int Row, int Col) unwrappedPos = GetUnwrappedPosition (idxRow, idxCol);
  3305. var ev = new RuneCellEventArgs (line, idxCol, unwrappedPos);
  3306. DrawNormalColor?.Invoke (this, ev);
  3307. if (line [idxCol].ColorScheme is { })
  3308. {
  3309. ColorScheme? colorScheme = line [idxCol].ColorScheme;
  3310. Driver.SetAttribute (Enabled ? colorScheme!.Focus : colorScheme!.Disabled);
  3311. }
  3312. else
  3313. {
  3314. Driver.SetAttribute (GetNormalColor ());
  3315. }
  3316. }
  3317. /// <summary>
  3318. /// Sets the <see cref="View.Driver"/> to an appropriate color for rendering the given <paramref name="idxCol"/>
  3319. /// of the current <paramref name="line"/>. Override to provide custom coloring by calling
  3320. /// <see cref="ConsoleDriver.SetAttribute(Attribute)"/> Defaults to <see cref="ColorScheme.Focus"/>.
  3321. /// </summary>
  3322. /// <param name="line">The line.</param>
  3323. /// <param name="idxCol">The col index.</param>
  3324. /// ///
  3325. /// <param name="idxRow">The row index.</param>
  3326. protected virtual void OnDrawReadOnlyColor (List<RuneCell> line, int idxCol, int idxRow)
  3327. {
  3328. (int Row, int Col) unwrappedPos = GetUnwrappedPosition (idxRow, idxCol);
  3329. var ev = new RuneCellEventArgs (line, idxCol, unwrappedPos);
  3330. DrawReadOnlyColor?.Invoke (this, ev);
  3331. ColorScheme? colorScheme = line [idxCol].ColorScheme is { } ? line [idxCol].ColorScheme : ColorScheme;
  3332. Attribute attribute;
  3333. if (colorScheme!.Disabled.Foreground == colorScheme.Focus.Background)
  3334. {
  3335. attribute = new Attribute (colorScheme.Focus.Foreground, colorScheme.Focus.Background);
  3336. }
  3337. else
  3338. {
  3339. attribute = new Attribute (colorScheme.Disabled.Foreground, colorScheme.Focus.Background);
  3340. }
  3341. Driver.SetAttribute (attribute);
  3342. }
  3343. /// <summary>
  3344. /// Sets the <see cref="View.Driver"/> to an appropriate color for rendering the given <paramref name="idxCol"/>
  3345. /// of the current <paramref name="line"/>. Override to provide custom coloring by calling
  3346. /// <see cref="ConsoleDriver.SetAttribute(Attribute)"/> Defaults to <see cref="ColorScheme.Focus"/>.
  3347. /// </summary>
  3348. /// <param name="line">The line.</param>
  3349. /// <param name="idxCol">The col index.</param>
  3350. /// ///
  3351. /// <param name="idxRow">The row index.</param>
  3352. protected virtual void OnDrawSelectionColor (List<RuneCell> line, int idxCol, int idxRow)
  3353. {
  3354. (int Row, int Col) unwrappedPos = GetUnwrappedPosition (idxRow, idxCol);
  3355. var ev = new RuneCellEventArgs (line, idxCol, unwrappedPos);
  3356. DrawSelectionColor?.Invoke (this, ev);
  3357. if (line [idxCol].ColorScheme is { })
  3358. {
  3359. ColorScheme? colorScheme = line [idxCol].ColorScheme;
  3360. Driver.SetAttribute (
  3361. new Attribute (colorScheme!.Focus.Background, colorScheme.Focus.Foreground)
  3362. );
  3363. }
  3364. else
  3365. {
  3366. Driver.SetAttribute (
  3367. new Attribute (
  3368. ColorScheme.Focus.Background,
  3369. ColorScheme.Focus.Foreground
  3370. )
  3371. );
  3372. }
  3373. }
  3374. /// <summary>
  3375. /// Sets the <see cref="View.Driver"/> to an appropriate color for rendering the given <paramref name="idxCol"/>
  3376. /// of the current <paramref name="line"/>. Override to provide custom coloring by calling
  3377. /// <see cref="ConsoleDriver.SetAttribute(Attribute)"/> Defaults to <see cref="ColorScheme.HotFocus"/>.
  3378. /// </summary>
  3379. /// <param name="line">The line.</param>
  3380. /// <param name="idxCol">The col index.</param>
  3381. /// ///
  3382. /// <param name="idxRow">The row index.</param>
  3383. protected virtual void OnDrawUsedColor (List<RuneCell> line, int idxCol, int idxRow)
  3384. {
  3385. (int Row, int Col) unwrappedPos = GetUnwrappedPosition (idxRow, idxCol);
  3386. var ev = new RuneCellEventArgs (line, idxCol, unwrappedPos);
  3387. DrawUsedColor?.Invoke (this, ev);
  3388. if (line [idxCol].ColorScheme is { })
  3389. {
  3390. ColorScheme? colorScheme = line [idxCol].ColorScheme;
  3391. SetValidUsedColor (colorScheme!);
  3392. }
  3393. else
  3394. {
  3395. SetValidUsedColor (ColorScheme);
  3396. }
  3397. }
  3398. /// <summary>
  3399. /// Sets the driver to the default color for the control where no text is being rendered. Defaults to
  3400. /// <see cref="ColorScheme.Normal"/>.
  3401. /// </summary>
  3402. protected virtual void SetNormalColor () { Driver.SetAttribute (GetNormalColor ()); }
  3403. private void Adjust ()
  3404. {
  3405. (int width, int height) offB = OffSetBackground ();
  3406. List<RuneCell> line = GetCurrentLine ();
  3407. bool need = NeedsDisplay || _wrapNeeded || !Used;
  3408. (int size, int length) tSize = TextModel.DisplaySize (line, -1, -1, false, TabWidth);
  3409. (int size, int length) dSize = TextModel.DisplaySize (line, _leftColumn, CurrentColumn, true, TabWidth);
  3410. if (!_wordWrap && CurrentColumn < _leftColumn)
  3411. {
  3412. _leftColumn = CurrentColumn;
  3413. need = true;
  3414. }
  3415. else if (!_wordWrap
  3416. && (CurrentColumn - _leftColumn + RightOffset > Frame.Width + offB.width || dSize.size + RightOffset >= Frame.Width + offB.width))
  3417. {
  3418. _leftColumn = TextModel.CalculateLeftColumn (
  3419. line,
  3420. _leftColumn,
  3421. CurrentColumn,
  3422. Frame.Width + offB.width - RightOffset,
  3423. TabWidth
  3424. );
  3425. need = true;
  3426. }
  3427. else if ((_wordWrap && _leftColumn > 0) || (dSize.size + RightOffset < Frame.Width + offB.width && tSize.size + RightOffset < Frame.Width + offB.width))
  3428. {
  3429. if (_leftColumn > 0)
  3430. {
  3431. _leftColumn = 0;
  3432. need = true;
  3433. }
  3434. }
  3435. if (CurrentRow < _topRow)
  3436. {
  3437. _topRow = CurrentRow;
  3438. need = true;
  3439. }
  3440. else if (CurrentRow - _topRow + BottomOffset >= Frame.Height + offB.height)
  3441. {
  3442. _topRow = Math.Min (Math.Max (CurrentRow - Frame.Height + 1 + BottomOffset, 0), CurrentRow);
  3443. need = true;
  3444. }
  3445. else if (_topRow > 0 && CurrentRow < _topRow)
  3446. {
  3447. _topRow = Math.Max (_topRow - 1, 0);
  3448. need = true;
  3449. }
  3450. if (need)
  3451. {
  3452. if (_wrapNeeded)
  3453. {
  3454. WrapTextModel ();
  3455. _wrapNeeded = false;
  3456. }
  3457. SetNeedsDisplay ();
  3458. }
  3459. else
  3460. {
  3461. PositionCursor ();
  3462. }
  3463. OnUnwrappedCursorPosition ();
  3464. }
  3465. private void AppendClipboard (string text) { Clipboard.Contents += text; }
  3466. private MenuBarItem BuildContextMenuBarItem ()
  3467. {
  3468. return new MenuBarItem (
  3469. new MenuItem []
  3470. {
  3471. new (
  3472. Strings.ctxSelectAll,
  3473. "",
  3474. SelectAll,
  3475. null,
  3476. null,
  3477. (KeyCode)KeyBindings.GetKeyFromCommands (Command.SelectAll)
  3478. ),
  3479. new (
  3480. Strings.ctxDeleteAll,
  3481. "",
  3482. DeleteAll,
  3483. null,
  3484. null,
  3485. (KeyCode)KeyBindings.GetKeyFromCommands (Command.DeleteAll)
  3486. ),
  3487. new (
  3488. Strings.ctxCopy,
  3489. "",
  3490. Copy,
  3491. null,
  3492. null,
  3493. (KeyCode)KeyBindings.GetKeyFromCommands (Command.Copy)
  3494. ),
  3495. new (
  3496. Strings.ctxCut,
  3497. "",
  3498. Cut,
  3499. null,
  3500. null,
  3501. (KeyCode)KeyBindings.GetKeyFromCommands (Command.Cut)
  3502. ),
  3503. new (
  3504. Strings.ctxPaste,
  3505. "",
  3506. Paste,
  3507. null,
  3508. null,
  3509. (KeyCode)KeyBindings.GetKeyFromCommands (Command.Paste)
  3510. ),
  3511. new (
  3512. Strings.ctxUndo,
  3513. "",
  3514. Undo,
  3515. null,
  3516. null,
  3517. (KeyCode)KeyBindings.GetKeyFromCommands (Command.Undo)
  3518. ),
  3519. new (
  3520. Strings.ctxRedo,
  3521. "",
  3522. Redo,
  3523. null,
  3524. null,
  3525. (KeyCode)KeyBindings.GetKeyFromCommands (Command.Redo)
  3526. )
  3527. }
  3528. );
  3529. }
  3530. private void ClearRegion (int left, int top, int right, int bottom)
  3531. {
  3532. for (int row = top; row < bottom; row++)
  3533. {
  3534. Move (left, row);
  3535. for (int col = left; col < right; col++)
  3536. {
  3537. AddRune (col, row, (Rune)' ');
  3538. }
  3539. }
  3540. }
  3541. //
  3542. // Clears the contents of the selected region
  3543. //
  3544. private void ClearRegion ()
  3545. {
  3546. SetWrapModel ();
  3547. long start, end;
  3548. long currentEncoded = ((long)(uint)CurrentRow << 32) | (uint)CurrentColumn;
  3549. GetEncodedRegionBounds (out start, out end);
  3550. var startRow = (int)(start >> 32);
  3551. var maxrow = (int)(end >> 32);
  3552. var startCol = (int)(start & 0xffffffff);
  3553. var endCol = (int)(end & 0xffffffff);
  3554. List<RuneCell> line = _model.GetLine (startRow);
  3555. _historyText.Add (new List<List<RuneCell>> { new (line) }, new Point (startCol, startRow));
  3556. List<List<RuneCell>> removedLines = new ();
  3557. if (startRow == maxrow)
  3558. {
  3559. removedLines.Add (new List<RuneCell> (line));
  3560. line.RemoveRange (startCol, endCol - startCol);
  3561. CurrentColumn = startCol;
  3562. if (_wordWrap)
  3563. {
  3564. SetNeedsDisplay ();
  3565. }
  3566. else
  3567. {
  3568. //QUESTION: Is the below comment still relevant?
  3569. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  3570. //SetNeedsDisplay (new (0, startRow - topRow, Frame.Width, startRow - topRow + 1));
  3571. SetNeedsDisplay ();
  3572. }
  3573. _historyText.Add (
  3574. new List<List<RuneCell>> (removedLines),
  3575. CursorPosition,
  3576. HistoryText.LineStatus.Removed
  3577. );
  3578. UpdateWrapModel ();
  3579. return;
  3580. }
  3581. removedLines.Add (new List<RuneCell> (line));
  3582. line.RemoveRange (startCol, line.Count - startCol);
  3583. List<RuneCell> line2 = _model.GetLine (maxrow);
  3584. line.AddRange (line2.Skip (endCol));
  3585. for (int row = startRow + 1; row <= maxrow; row++)
  3586. {
  3587. removedLines.Add (new List<RuneCell> (_model.GetLine (startRow + 1)));
  3588. _model.RemoveLine (startRow + 1);
  3589. }
  3590. if (currentEncoded == end)
  3591. {
  3592. CurrentRow -= maxrow - startRow;
  3593. }
  3594. CurrentColumn = startCol;
  3595. _historyText.Add (
  3596. new List<List<RuneCell>> (removedLines),
  3597. CursorPosition,
  3598. HistoryText.LineStatus.Removed
  3599. );
  3600. UpdateWrapModel ();
  3601. SetNeedsDisplay ();
  3602. }
  3603. private void ClearSelectedRegion ()
  3604. {
  3605. SetWrapModel ();
  3606. if (!_isReadOnly)
  3607. {
  3608. ClearRegion ();
  3609. }
  3610. UpdateWrapModel ();
  3611. Selecting = false;
  3612. DoNeededAction ();
  3613. }
  3614. private void ContextMenu_KeyChanged (object sender, KeyChangedEventArgs e) { KeyBindings.Replace (e.OldKey, e.NewKey); }
  3615. private bool DeleteTextBackwards ()
  3616. {
  3617. SetWrapModel ();
  3618. if (CurrentColumn > 0)
  3619. {
  3620. // Delete backwards
  3621. List<RuneCell> currentLine = GetCurrentLine ();
  3622. _historyText.Add (new List<List<RuneCell>> { new (currentLine) }, CursorPosition);
  3623. currentLine.RemoveAt (CurrentColumn - 1);
  3624. if (_wordWrap)
  3625. {
  3626. _wrapNeeded = true;
  3627. }
  3628. CurrentColumn--;
  3629. _historyText.Add (
  3630. new List<List<RuneCell>> { new (currentLine) },
  3631. CursorPosition,
  3632. HistoryText.LineStatus.Replaced
  3633. );
  3634. if (CurrentColumn < _leftColumn)
  3635. {
  3636. _leftColumn--;
  3637. SetNeedsDisplay ();
  3638. }
  3639. else
  3640. {
  3641. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  3642. //SetNeedsDisplay (new (0, currentRow - topRow, 1, Frame.Width));
  3643. SetNeedsDisplay ();
  3644. }
  3645. }
  3646. else
  3647. {
  3648. // Merges the current line with the previous one.
  3649. if (CurrentRow == 0)
  3650. {
  3651. return true;
  3652. }
  3653. int prowIdx = CurrentRow - 1;
  3654. List<RuneCell> prevRow = _model.GetLine (prowIdx);
  3655. _historyText.Add (new List<List<RuneCell>> { new (prevRow) }, CursorPosition);
  3656. List<List<RuneCell>> removedLines = new () { new List<RuneCell> (prevRow) };
  3657. removedLines.Add (new List<RuneCell> (GetCurrentLine ()));
  3658. _historyText.Add (
  3659. removedLines,
  3660. new Point (CurrentColumn, prowIdx),
  3661. HistoryText.LineStatus.Removed
  3662. );
  3663. int prevCount = prevRow.Count;
  3664. _model.GetLine (prowIdx).AddRange (GetCurrentLine ());
  3665. _model.RemoveLine (CurrentRow);
  3666. if (_wordWrap)
  3667. {
  3668. _wrapNeeded = true;
  3669. }
  3670. CurrentRow--;
  3671. _historyText.Add (
  3672. new List<List<RuneCell>> { GetCurrentLine () },
  3673. new Point (CurrentColumn, prowIdx),
  3674. HistoryText.LineStatus.Replaced
  3675. );
  3676. CurrentColumn = prevCount;
  3677. SetNeedsDisplay ();
  3678. }
  3679. UpdateWrapModel ();
  3680. return false;
  3681. }
  3682. private bool DeleteTextForwards ()
  3683. {
  3684. SetWrapModel ();
  3685. List<RuneCell> currentLine = GetCurrentLine ();
  3686. if (CurrentColumn == currentLine.Count)
  3687. {
  3688. if (CurrentRow + 1 == _model.Count)
  3689. {
  3690. UpdateWrapModel ();
  3691. return true;
  3692. }
  3693. _historyText.Add (new List<List<RuneCell>> { new (currentLine) }, CursorPosition);
  3694. List<List<RuneCell>> removedLines = new () { new List<RuneCell> (currentLine) };
  3695. List<RuneCell> nextLine = _model.GetLine (CurrentRow + 1);
  3696. removedLines.Add (new List<RuneCell> (nextLine));
  3697. _historyText.Add (removedLines, CursorPosition, HistoryText.LineStatus.Removed);
  3698. currentLine.AddRange (nextLine);
  3699. _model.RemoveLine (CurrentRow + 1);
  3700. _historyText.Add (
  3701. new List<List<RuneCell>> { new (currentLine) },
  3702. CursorPosition,
  3703. HistoryText.LineStatus.Replaced
  3704. );
  3705. if (_wordWrap)
  3706. {
  3707. _wrapNeeded = true;
  3708. }
  3709. DoSetNeedsDisplay (new (0, CurrentRow - _topRow, Frame.Width, CurrentRow - _topRow + 1));
  3710. }
  3711. else
  3712. {
  3713. _historyText.Add ([[..currentLine]], CursorPosition);
  3714. currentLine.RemoveAt (CurrentColumn);
  3715. _historyText.Add (
  3716. [[..currentLine]],
  3717. CursorPosition,
  3718. HistoryText.LineStatus.Replaced
  3719. );
  3720. if (_wordWrap)
  3721. {
  3722. _wrapNeeded = true;
  3723. }
  3724. DoSetNeedsDisplay (
  3725. new (
  3726. CurrentColumn - _leftColumn,
  3727. CurrentRow - _topRow,
  3728. Frame.Width,
  3729. CurrentRow - _topRow + 1
  3730. )
  3731. );
  3732. }
  3733. UpdateWrapModel ();
  3734. return false;
  3735. }
  3736. private void DoNeededAction ()
  3737. {
  3738. if (NeedsDisplay)
  3739. {
  3740. Adjust ();
  3741. }
  3742. else
  3743. {
  3744. PositionCursor ();
  3745. }
  3746. }
  3747. private void DoSetNeedsDisplay (Rectangle rect)
  3748. {
  3749. if (_wrapNeeded)
  3750. {
  3751. SetNeedsDisplay ();
  3752. }
  3753. else
  3754. {
  3755. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  3756. //SetNeedsDisplay (rect);
  3757. SetNeedsDisplay ();
  3758. }
  3759. }
  3760. private IEnumerable<(int col, int row, RuneCell rune)> ForwardIterator (int col, int row)
  3761. {
  3762. if (col < 0 || row < 0)
  3763. {
  3764. yield break;
  3765. }
  3766. if (row >= _model.Count)
  3767. {
  3768. yield break;
  3769. }
  3770. List<RuneCell> line = GetCurrentLine ();
  3771. if (col >= line.Count)
  3772. {
  3773. yield break;
  3774. }
  3775. while (row < _model.Count)
  3776. {
  3777. for (int c = col; c < line.Count; c++)
  3778. {
  3779. yield return (c, row, line [c]);
  3780. }
  3781. col = 0;
  3782. row++;
  3783. line = GetCurrentLine ();
  3784. }
  3785. }
  3786. private void GenerateSuggestions ()
  3787. {
  3788. List<RuneCell> currentLine = GetCurrentLine ();
  3789. int cursorPosition = Math.Min (CurrentColumn, currentLine.Count);
  3790. Autocomplete.Context = new AutocompleteContext (
  3791. currentLine,
  3792. cursorPosition,
  3793. Autocomplete.Context != null
  3794. ? Autocomplete.Context.Canceled
  3795. : false
  3796. );
  3797. Autocomplete.GenerateSuggestions (
  3798. Autocomplete.Context
  3799. );
  3800. }
  3801. // Returns an encoded region start..end (top 32 bits are the row, low32 the column)
  3802. private void GetEncodedRegionBounds (
  3803. out long start,
  3804. out long end,
  3805. int? startRow = null,
  3806. int? startCol = null,
  3807. int? cRow = null,
  3808. int? cCol = null
  3809. )
  3810. {
  3811. long selection;
  3812. long point;
  3813. if (startRow is null || startCol is null || cRow is null || cCol is null)
  3814. {
  3815. selection = ((long)(uint)_selectionStartRow << 32) | (uint)_selectionStartColumn;
  3816. point = ((long)(uint)CurrentRow << 32) | (uint)CurrentColumn;
  3817. }
  3818. else
  3819. {
  3820. selection = ((long)(uint)startRow << 32) | (uint)startCol;
  3821. point = ((long)(uint)cRow << 32) | (uint)cCol;
  3822. }
  3823. if (selection > point)
  3824. {
  3825. start = point;
  3826. end = selection;
  3827. }
  3828. else
  3829. {
  3830. start = selection;
  3831. end = point;
  3832. }
  3833. }
  3834. //
  3835. // Returns a string with the text in the selected
  3836. // region.
  3837. //
  3838. private string GetRegion (
  3839. int? sRow = null,
  3840. int? sCol = null,
  3841. int? cRow = null,
  3842. int? cCol = null,
  3843. TextModel? model = null
  3844. )
  3845. {
  3846. long start, end;
  3847. GetEncodedRegionBounds (out start, out end, sRow, sCol, cRow, cCol);
  3848. if (start == end)
  3849. {
  3850. return string.Empty;
  3851. }
  3852. var startRow = (int)(start >> 32);
  3853. var maxrow = (int)(end >> 32);
  3854. var startCol = (int)(start & 0xffffffff);
  3855. var endCol = (int)(end & 0xffffffff);
  3856. List<RuneCell> line = model is null ? _model.GetLine (startRow) : model.GetLine (startRow);
  3857. if (startRow == maxrow)
  3858. {
  3859. return StringFromRunes (line.GetRange (startCol, endCol - startCol));
  3860. }
  3861. string res = StringFromRunes (line.GetRange (startCol, line.Count - startCol));
  3862. for (int row = startRow + 1; row < maxrow; row++)
  3863. {
  3864. res = res
  3865. + Environment.NewLine
  3866. + StringFromRunes (
  3867. model == null
  3868. ? _model.GetLine (row)
  3869. : model.GetLine (row)
  3870. );
  3871. }
  3872. line = model is null ? _model.GetLine (maxrow) : model.GetLine (maxrow);
  3873. res = res + Environment.NewLine + StringFromRunes (line.GetRange (0, endCol));
  3874. return res;
  3875. }
  3876. private int GetSelectedLength () { return SelectedText.Length; }
  3877. private string GetSelectedRegion ()
  3878. {
  3879. int cRow = CurrentRow;
  3880. int cCol = CurrentColumn;
  3881. int startRow = _selectionStartRow;
  3882. int startCol = _selectionStartColumn;
  3883. TextModel model = _model;
  3884. if (_wordWrap)
  3885. {
  3886. cRow = _wrapManager!.GetModelLineFromWrappedLines (CurrentRow);
  3887. cCol = _wrapManager.GetModelColFromWrappedLines (CurrentRow, CurrentColumn);
  3888. startRow = _wrapManager.GetModelLineFromWrappedLines (_selectionStartRow);
  3889. startCol = _wrapManager.GetModelColFromWrappedLines (_selectionStartRow, _selectionStartColumn);
  3890. model = _wrapManager.Model;
  3891. }
  3892. OnUnwrappedCursorPosition (cRow, cCol);
  3893. return GetRegion (startRow, startCol, cRow, cCol, model);
  3894. }
  3895. private (int Row, int Col) GetUnwrappedPosition (int line, int col)
  3896. {
  3897. if (WordWrap)
  3898. {
  3899. return new ValueTuple<int, int> (
  3900. _wrapManager!.GetModelLineFromWrappedLines (line),
  3901. _wrapManager.GetModelColFromWrappedLines (line, col)
  3902. );
  3903. }
  3904. return new ValueTuple<int, int> (line, col);
  3905. }
  3906. private void HistoryText_ChangeText (object sender, HistoryText.HistoryTextItem obj)
  3907. {
  3908. SetWrapModel ();
  3909. if (obj is { })
  3910. {
  3911. int startLine = obj.CursorPosition.Y;
  3912. if (obj.RemovedOnAdded is { })
  3913. {
  3914. int offset;
  3915. if (obj.IsUndoing)
  3916. {
  3917. offset = Math.Max (obj.RemovedOnAdded.Lines.Count - obj.Lines.Count, 1);
  3918. }
  3919. else
  3920. {
  3921. offset = obj.RemovedOnAdded.Lines.Count - 1;
  3922. }
  3923. for (var i = 0; i < offset; i++)
  3924. {
  3925. if (Lines > obj.RemovedOnAdded.CursorPosition.Y)
  3926. {
  3927. _model.RemoveLine (obj.RemovedOnAdded.CursorPosition.Y);
  3928. }
  3929. else
  3930. {
  3931. break;
  3932. }
  3933. }
  3934. }
  3935. for (var i = 0; i < obj.Lines.Count; i++)
  3936. {
  3937. if (i == 0)
  3938. {
  3939. _model.ReplaceLine (startLine, obj.Lines [i]);
  3940. }
  3941. else if ((obj.IsUndoing && obj.LineStatus == HistoryText.LineStatus.Removed)
  3942. || (!obj.IsUndoing && obj.LineStatus == HistoryText.LineStatus.Added))
  3943. {
  3944. _model.AddLine (startLine, obj.Lines [i]);
  3945. }
  3946. else if (Lines > obj.CursorPosition.Y + 1)
  3947. {
  3948. _model.RemoveLine (obj.CursorPosition.Y + 1);
  3949. }
  3950. startLine++;
  3951. }
  3952. CursorPosition = obj.FinalCursorPosition;
  3953. }
  3954. UpdateWrapModel ();
  3955. Adjust ();
  3956. OnContentsChanged ();
  3957. }
  3958. private void Insert (RuneCell cell)
  3959. {
  3960. List<RuneCell> line = GetCurrentLine ();
  3961. if (Used)
  3962. {
  3963. line.Insert (Math.Min (CurrentColumn, line.Count), cell);
  3964. }
  3965. else
  3966. {
  3967. if (CurrentColumn < line.Count)
  3968. {
  3969. line.RemoveAt (CurrentColumn);
  3970. }
  3971. line.Insert (Math.Min (CurrentColumn, line.Count), cell);
  3972. }
  3973. int prow = CurrentRow - _topRow;
  3974. if (!_wrapNeeded)
  3975. {
  3976. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  3977. //SetNeedsDisplay (new (0, prow, Math.Max (Frame.Width, 0), Math.Max (prow + 1, 0)));
  3978. SetNeedsDisplay ();
  3979. }
  3980. }
  3981. private void InsertAllText (string text)
  3982. {
  3983. if (string.IsNullOrEmpty (text))
  3984. {
  3985. return;
  3986. }
  3987. List<List<RuneCell>> lines = TextModel.StringToLinesOfRuneCells (text);
  3988. if (lines.Count == 0)
  3989. {
  3990. return;
  3991. }
  3992. SetWrapModel ();
  3993. List<RuneCell> line = GetCurrentLine ();
  3994. _historyText.Add (new List<List<RuneCell>> { new (line) }, CursorPosition);
  3995. // Optimize single line
  3996. if (lines.Count == 1)
  3997. {
  3998. line.InsertRange (CurrentColumn, lines [0]);
  3999. CurrentColumn += lines [0].Count;
  4000. _historyText.Add (
  4001. new List<List<RuneCell>> { new (line) },
  4002. CursorPosition,
  4003. HistoryText.LineStatus.Replaced
  4004. );
  4005. if (!_wordWrap && CurrentColumn - _leftColumn > Frame.Width)
  4006. {
  4007. _leftColumn = Math.Max (CurrentColumn - Frame.Width + 1, 0);
  4008. }
  4009. if (_wordWrap)
  4010. {
  4011. SetNeedsDisplay ();
  4012. }
  4013. else
  4014. {
  4015. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  4016. //SetNeedsDisplay (new (0, currentRow - topRow, Frame.Width, Math.Max (currentRow - topRow + 1, 0)));
  4017. SetNeedsDisplay ();
  4018. }
  4019. UpdateWrapModel ();
  4020. OnContentsChanged ();
  4021. return;
  4022. }
  4023. List<RuneCell>? rest = null;
  4024. var lastp = 0;
  4025. if (_model.Count > 0 && line.Count > 0 && !_copyWithoutSelection)
  4026. {
  4027. // Keep a copy of the rest of the line
  4028. int restCount = line.Count - CurrentColumn;
  4029. rest = line.GetRange (CurrentColumn, restCount);
  4030. line.RemoveRange (CurrentColumn, restCount);
  4031. }
  4032. // First line is inserted at the current location, the rest is appended
  4033. line.InsertRange (CurrentColumn, lines [0]);
  4034. //model.AddLine (currentRow, lines [0]);
  4035. List<List<RuneCell>> addedLines = new () { new List<RuneCell> (line) };
  4036. for (var i = 1; i < lines.Count; i++)
  4037. {
  4038. _model.AddLine (CurrentRow + i, lines [i]);
  4039. addedLines.Add (new List<RuneCell> (lines [i]));
  4040. }
  4041. if (rest is { })
  4042. {
  4043. List<RuneCell> last = _model.GetLine (CurrentRow + lines.Count - 1);
  4044. lastp = last.Count;
  4045. last.InsertRange (last.Count, rest);
  4046. addedLines.Last ().InsertRange (addedLines.Last ().Count, rest);
  4047. }
  4048. _historyText.Add (addedLines, CursorPosition, HistoryText.LineStatus.Added);
  4049. // Now adjust column and row positions
  4050. CurrentRow += lines.Count - 1;
  4051. CurrentColumn = rest is { } ? lastp : lines [lines.Count - 1].Count;
  4052. Adjust ();
  4053. _historyText.Add (
  4054. new List<List<RuneCell>> { new (line) },
  4055. CursorPosition,
  4056. HistoryText.LineStatus.Replaced
  4057. );
  4058. UpdateWrapModel ();
  4059. OnContentsChanged ();
  4060. }
  4061. private bool InsertText (Key a, ColorScheme? colorScheme = null)
  4062. {
  4063. //So that special keys like tab can be processed
  4064. if (_isReadOnly)
  4065. {
  4066. return true;
  4067. }
  4068. SetWrapModel ();
  4069. _historyText.Add (new List<List<RuneCell>> { new (GetCurrentLine ()) }, CursorPosition);
  4070. if (Selecting)
  4071. {
  4072. ClearSelectedRegion ();
  4073. }
  4074. if ((uint)a.KeyCode == '\n')
  4075. {
  4076. _model.AddLine (CurrentRow + 1, new List<RuneCell> ());
  4077. CurrentRow++;
  4078. CurrentColumn = 0;
  4079. }
  4080. else if ((uint)a.KeyCode == '\r')
  4081. {
  4082. CurrentColumn = 0;
  4083. }
  4084. else
  4085. {
  4086. if (Used)
  4087. {
  4088. Insert (new RuneCell { Rune = a.AsRune, ColorScheme = colorScheme });
  4089. CurrentColumn++;
  4090. if (CurrentColumn >= _leftColumn + Frame.Width)
  4091. {
  4092. _leftColumn++;
  4093. SetNeedsDisplay ();
  4094. }
  4095. }
  4096. else
  4097. {
  4098. Insert (new RuneCell { Rune = a.AsRune, ColorScheme = colorScheme });
  4099. CurrentColumn++;
  4100. }
  4101. }
  4102. _historyText.Add (
  4103. new List<List<RuneCell>> { new (GetCurrentLine ()) },
  4104. CursorPosition,
  4105. HistoryText.LineStatus.Replaced
  4106. );
  4107. UpdateWrapModel ();
  4108. OnContentsChanged ();
  4109. return true;
  4110. }
  4111. private void KillToEndOfLine ()
  4112. {
  4113. if (_isReadOnly)
  4114. {
  4115. return;
  4116. }
  4117. if (_model.Count == 1 && GetCurrentLine ().Count == 0)
  4118. {
  4119. // Prevents from adding line feeds if there is no more lines.
  4120. return;
  4121. }
  4122. SetWrapModel ();
  4123. List<RuneCell> currentLine = GetCurrentLine ();
  4124. var setLastWasKill = true;
  4125. if (currentLine.Count > 0 && CurrentColumn == currentLine.Count)
  4126. {
  4127. UpdateWrapModel ();
  4128. DeleteTextForwards ();
  4129. return;
  4130. }
  4131. _historyText.Add (new List<List<RuneCell>> { new (currentLine) }, CursorPosition);
  4132. if (currentLine.Count == 0)
  4133. {
  4134. if (CurrentRow < _model.Count - 1)
  4135. {
  4136. List<List<RuneCell>> removedLines = new () { new List<RuneCell> (currentLine) };
  4137. _model.RemoveLine (CurrentRow);
  4138. removedLines.Add (new List<RuneCell> (GetCurrentLine ()));
  4139. _historyText.Add (
  4140. new List<List<RuneCell>> (removedLines),
  4141. CursorPosition,
  4142. HistoryText.LineStatus.Removed
  4143. );
  4144. }
  4145. if (_model.Count > 0 || _lastWasKill)
  4146. {
  4147. string val = Environment.NewLine;
  4148. if (_lastWasKill)
  4149. {
  4150. AppendClipboard (val);
  4151. }
  4152. else
  4153. {
  4154. SetClipboard (val);
  4155. }
  4156. }
  4157. if (_model.Count == 0)
  4158. {
  4159. // Prevents from adding line feeds if there is no more lines.
  4160. setLastWasKill = false;
  4161. }
  4162. }
  4163. else
  4164. {
  4165. int restCount = currentLine.Count - CurrentColumn;
  4166. List<RuneCell> rest = currentLine.GetRange (CurrentColumn, restCount);
  4167. var val = string.Empty;
  4168. val += StringFromRunes (rest);
  4169. if (_lastWasKill)
  4170. {
  4171. AppendClipboard (val);
  4172. }
  4173. else
  4174. {
  4175. SetClipboard (val);
  4176. }
  4177. currentLine.RemoveRange (CurrentColumn, restCount);
  4178. }
  4179. _historyText.Add (
  4180. [[..GetCurrentLine ()]],
  4181. CursorPosition,
  4182. HistoryText.LineStatus.Replaced
  4183. );
  4184. UpdateWrapModel ();
  4185. DoSetNeedsDisplay (new (0, CurrentRow - _topRow, Frame.Width, Frame.Height));
  4186. _lastWasKill = setLastWasKill;
  4187. DoNeededAction ();
  4188. }
  4189. private void KillToStartOfLine ()
  4190. {
  4191. if (_isReadOnly)
  4192. {
  4193. return;
  4194. }
  4195. if (_model.Count == 1 && GetCurrentLine ().Count == 0)
  4196. {
  4197. // Prevents from adding line feeds if there is no more lines.
  4198. return;
  4199. }
  4200. SetWrapModel ();
  4201. List<RuneCell> currentLine = GetCurrentLine ();
  4202. var setLastWasKill = true;
  4203. if (currentLine.Count > 0 && CurrentColumn == 0)
  4204. {
  4205. UpdateWrapModel ();
  4206. DeleteTextBackwards ();
  4207. return;
  4208. }
  4209. _historyText.Add ([[..currentLine]], CursorPosition);
  4210. if (currentLine.Count == 0)
  4211. {
  4212. if (CurrentRow > 0)
  4213. {
  4214. _model.RemoveLine (CurrentRow);
  4215. if (_model.Count > 0 || _lastWasKill)
  4216. {
  4217. string val = Environment.NewLine;
  4218. if (_lastWasKill)
  4219. {
  4220. AppendClipboard (val);
  4221. }
  4222. else
  4223. {
  4224. SetClipboard (val);
  4225. }
  4226. }
  4227. if (_model.Count == 0)
  4228. {
  4229. // Prevents from adding line feeds if there is no more lines.
  4230. setLastWasKill = false;
  4231. }
  4232. CurrentRow--;
  4233. currentLine = _model.GetLine (CurrentRow);
  4234. List<List<RuneCell>> removedLine =
  4235. [
  4236. [..currentLine],
  4237. []
  4238. ];
  4239. _historyText.Add (
  4240. [..removedLine],
  4241. CursorPosition,
  4242. HistoryText.LineStatus.Removed
  4243. );
  4244. CurrentColumn = currentLine.Count;
  4245. }
  4246. }
  4247. else
  4248. {
  4249. int restCount = CurrentColumn;
  4250. List<RuneCell> rest = currentLine.GetRange (0, restCount);
  4251. var val = string.Empty;
  4252. val += StringFromRunes (rest);
  4253. if (_lastWasKill)
  4254. {
  4255. AppendClipboard (val);
  4256. }
  4257. else
  4258. {
  4259. SetClipboard (val);
  4260. }
  4261. currentLine.RemoveRange (0, restCount);
  4262. CurrentColumn = 0;
  4263. }
  4264. _historyText.Add (
  4265. [[..GetCurrentLine ()]],
  4266. CursorPosition,
  4267. HistoryText.LineStatus.Replaced
  4268. );
  4269. UpdateWrapModel ();
  4270. DoSetNeedsDisplay (new (0, CurrentRow - _topRow, Frame.Width, Frame.Height));
  4271. _lastWasKill = setLastWasKill;
  4272. DoNeededAction ();
  4273. }
  4274. private void KillWordBackward ()
  4275. {
  4276. if (_isReadOnly)
  4277. {
  4278. return;
  4279. }
  4280. SetWrapModel ();
  4281. List<RuneCell> currentLine = GetCurrentLine ();
  4282. _historyText.Add ([[..GetCurrentLine ()]], CursorPosition);
  4283. if (CurrentColumn == 0)
  4284. {
  4285. DeleteTextBackwards ();
  4286. _historyText.ReplaceLast (
  4287. [[..GetCurrentLine ()]],
  4288. CursorPosition,
  4289. HistoryText.LineStatus.Replaced
  4290. );
  4291. UpdateWrapModel ();
  4292. return;
  4293. }
  4294. (int col, int row)? newPos = _model.WordBackward (CurrentColumn, CurrentRow);
  4295. if (newPos.HasValue && CurrentRow == newPos.Value.row)
  4296. {
  4297. int restCount = CurrentColumn - newPos.Value.col;
  4298. currentLine.RemoveRange (newPos.Value.col, restCount);
  4299. if (_wordWrap)
  4300. {
  4301. _wrapNeeded = true;
  4302. }
  4303. CurrentColumn = newPos.Value.col;
  4304. }
  4305. else if (newPos.HasValue)
  4306. {
  4307. int restCount = currentLine.Count - CurrentColumn;
  4308. currentLine.RemoveRange (CurrentColumn, restCount);
  4309. if (_wordWrap)
  4310. {
  4311. _wrapNeeded = true;
  4312. }
  4313. CurrentColumn = newPos.Value.col;
  4314. CurrentRow = newPos.Value.row;
  4315. }
  4316. _historyText.Add (
  4317. [[..GetCurrentLine ()]],
  4318. CursorPosition,
  4319. HistoryText.LineStatus.Replaced
  4320. );
  4321. UpdateWrapModel ();
  4322. DoSetNeedsDisplay (new (0, CurrentRow - _topRow, Frame.Width, Frame.Height));
  4323. DoNeededAction ();
  4324. }
  4325. private void KillWordForward ()
  4326. {
  4327. if (_isReadOnly)
  4328. {
  4329. return;
  4330. }
  4331. SetWrapModel ();
  4332. List<RuneCell> currentLine = GetCurrentLine ();
  4333. _historyText.Add ([[..GetCurrentLine ()]], CursorPosition);
  4334. if (currentLine.Count == 0 || CurrentColumn == currentLine.Count)
  4335. {
  4336. DeleteTextForwards ();
  4337. _historyText.ReplaceLast (
  4338. [[..GetCurrentLine ()]],
  4339. CursorPosition,
  4340. HistoryText.LineStatus.Replaced
  4341. );
  4342. UpdateWrapModel ();
  4343. return;
  4344. }
  4345. (int col, int row)? newPos = _model.WordForward (CurrentColumn, CurrentRow);
  4346. var restCount = 0;
  4347. if (newPos.HasValue && CurrentRow == newPos.Value.row)
  4348. {
  4349. restCount = newPos.Value.col - CurrentColumn;
  4350. currentLine.RemoveRange (CurrentColumn, restCount);
  4351. }
  4352. else if (newPos.HasValue)
  4353. {
  4354. restCount = currentLine.Count - CurrentColumn;
  4355. currentLine.RemoveRange (CurrentColumn, restCount);
  4356. }
  4357. if (_wordWrap)
  4358. {
  4359. _wrapNeeded = true;
  4360. }
  4361. _historyText.Add (
  4362. [[..GetCurrentLine ()]],
  4363. CursorPosition,
  4364. HistoryText.LineStatus.Replaced
  4365. );
  4366. UpdateWrapModel ();
  4367. DoSetNeedsDisplay (new (0, CurrentRow - _topRow, Frame.Width, Frame.Height));
  4368. DoNeededAction ();
  4369. }
  4370. private void Model_LinesLoaded (object sender, EventArgs e)
  4371. {
  4372. // This call is not needed. Model_LinesLoaded gets invoked when
  4373. // model.LoadString (value) is called. LoadString is called from one place
  4374. // (Text.set) and historyText.Clear() is called immediately after.
  4375. // If this call happens, HistoryText_ChangeText will get called multiple times
  4376. // when Text is set, which is wrong.
  4377. //historyText.Clear (Text);
  4378. if (!_multiline && !IsInitialized)
  4379. {
  4380. CurrentColumn = Text.GetRuneCount ();
  4381. _leftColumn = CurrentColumn > Frame.Width + 1 ? CurrentColumn - Frame.Width + 1 : 0;
  4382. }
  4383. }
  4384. private void MoveBottomEnd ()
  4385. {
  4386. ResetAllTrack ();
  4387. if (_shiftSelecting && Selecting)
  4388. {
  4389. StopSelecting ();
  4390. }
  4391. MoveEnd ();
  4392. }
  4393. private void MoveBottomEndExtend ()
  4394. {
  4395. ResetAllTrack ();
  4396. StartSelecting ();
  4397. MoveEnd ();
  4398. }
  4399. private void MoveDown ()
  4400. {
  4401. if (CurrentRow + 1 < _model.Count)
  4402. {
  4403. if (_columnTrack == -1)
  4404. {
  4405. _columnTrack = CurrentColumn;
  4406. }
  4407. CurrentRow++;
  4408. if (CurrentRow + BottomOffset >= _topRow + Frame.Height)
  4409. {
  4410. _topRow++;
  4411. SetNeedsDisplay ();
  4412. }
  4413. TrackColumn ();
  4414. PositionCursor ();
  4415. }
  4416. else if (CurrentRow > Frame.Height)
  4417. {
  4418. Adjust ();
  4419. }
  4420. DoNeededAction ();
  4421. }
  4422. private void MoveEndOfLine ()
  4423. {
  4424. List<RuneCell> currentLine = GetCurrentLine ();
  4425. CurrentColumn = currentLine.Count;
  4426. Adjust ();
  4427. DoNeededAction ();
  4428. }
  4429. private void MoveLeft ()
  4430. {
  4431. if (CurrentColumn > 0)
  4432. {
  4433. CurrentColumn--;
  4434. }
  4435. else
  4436. {
  4437. if (CurrentRow > 0)
  4438. {
  4439. CurrentRow--;
  4440. if (CurrentRow < _topRow)
  4441. {
  4442. _topRow--;
  4443. SetNeedsDisplay ();
  4444. }
  4445. List<RuneCell> currentLine = GetCurrentLine ();
  4446. CurrentColumn = currentLine.Count;
  4447. }
  4448. }
  4449. Adjust ();
  4450. DoNeededAction ();
  4451. }
  4452. private bool MoveNextView ()
  4453. {
  4454. if (Application.OverlappedTop is { })
  4455. {
  4456. return SuperView?.FocusNext () == true;
  4457. }
  4458. return false;
  4459. }
  4460. private void MovePageDown ()
  4461. {
  4462. int nPageDnShift = Frame.Height - 1;
  4463. if (CurrentRow >= 0 && CurrentRow < _model.Count)
  4464. {
  4465. if (_columnTrack == -1)
  4466. {
  4467. _columnTrack = CurrentColumn;
  4468. }
  4469. CurrentRow = CurrentRow + nPageDnShift > _model.Count
  4470. ? _model.Count > 0 ? _model.Count - 1 : 0
  4471. : CurrentRow + nPageDnShift;
  4472. if (_topRow < CurrentRow - nPageDnShift)
  4473. {
  4474. _topRow = CurrentRow >= _model.Count
  4475. ? CurrentRow - nPageDnShift
  4476. : _topRow + nPageDnShift;
  4477. SetNeedsDisplay ();
  4478. }
  4479. TrackColumn ();
  4480. PositionCursor ();
  4481. }
  4482. DoNeededAction ();
  4483. }
  4484. private void MovePageUp ()
  4485. {
  4486. int nPageUpShift = Frame.Height - 1;
  4487. if (CurrentRow > 0)
  4488. {
  4489. if (_columnTrack == -1)
  4490. {
  4491. _columnTrack = CurrentColumn;
  4492. }
  4493. CurrentRow = CurrentRow - nPageUpShift < 0 ? 0 : CurrentRow - nPageUpShift;
  4494. if (CurrentRow < _topRow)
  4495. {
  4496. _topRow = _topRow - nPageUpShift < 0 ? 0 : _topRow - nPageUpShift;
  4497. SetNeedsDisplay ();
  4498. }
  4499. TrackColumn ();
  4500. PositionCursor ();
  4501. }
  4502. DoNeededAction ();
  4503. }
  4504. private bool MovePreviousView ()
  4505. {
  4506. if (Application.OverlappedTop is { })
  4507. {
  4508. return SuperView?.FocusPrev () == true;
  4509. }
  4510. return false;
  4511. }
  4512. private void MoveRight ()
  4513. {
  4514. List<RuneCell> currentLine = GetCurrentLine ();
  4515. if (CurrentColumn < currentLine.Count)
  4516. {
  4517. CurrentColumn++;
  4518. }
  4519. else
  4520. {
  4521. if (CurrentRow + 1 < _model.Count)
  4522. {
  4523. CurrentRow++;
  4524. CurrentColumn = 0;
  4525. if (CurrentRow >= _topRow + Frame.Height)
  4526. {
  4527. _topRow++;
  4528. SetNeedsDisplay ();
  4529. }
  4530. }
  4531. }
  4532. Adjust ();
  4533. DoNeededAction ();
  4534. }
  4535. private void MoveStartOfLine ()
  4536. {
  4537. if (_leftColumn > 0)
  4538. {
  4539. SetNeedsDisplay ();
  4540. }
  4541. CurrentColumn = 0;
  4542. _leftColumn = 0;
  4543. Adjust ();
  4544. DoNeededAction ();
  4545. }
  4546. private void MoveTopHome ()
  4547. {
  4548. ResetAllTrack ();
  4549. if (_shiftSelecting && Selecting)
  4550. {
  4551. StopSelecting ();
  4552. }
  4553. MoveHome ();
  4554. }
  4555. private void MoveTopHomeExtend ()
  4556. {
  4557. ResetColumnTrack ();
  4558. StartSelecting ();
  4559. MoveHome ();
  4560. }
  4561. private void MoveUp ()
  4562. {
  4563. if (CurrentRow > 0)
  4564. {
  4565. if (_columnTrack == -1)
  4566. {
  4567. _columnTrack = CurrentColumn;
  4568. }
  4569. CurrentRow--;
  4570. if (CurrentRow < _topRow)
  4571. {
  4572. _topRow--;
  4573. SetNeedsDisplay ();
  4574. }
  4575. TrackColumn ();
  4576. PositionCursor ();
  4577. }
  4578. DoNeededAction ();
  4579. }
  4580. private void MoveWordBackward ()
  4581. {
  4582. (int col, int row)? newPos = _model.WordBackward (CurrentColumn, CurrentRow);
  4583. if (newPos.HasValue)
  4584. {
  4585. CurrentColumn = newPos.Value.col;
  4586. CurrentRow = newPos.Value.row;
  4587. }
  4588. Adjust ();
  4589. DoNeededAction ();
  4590. }
  4591. private void MoveWordForward ()
  4592. {
  4593. (int col, int row)? newPos = _model.WordForward (CurrentColumn, CurrentRow);
  4594. if (newPos.HasValue)
  4595. {
  4596. CurrentColumn = newPos.Value.col;
  4597. CurrentRow = newPos.Value.row;
  4598. }
  4599. Adjust ();
  4600. DoNeededAction ();
  4601. }
  4602. private (int width, int height) OffSetBackground ()
  4603. {
  4604. var w = 0;
  4605. var h = 0;
  4606. if (SuperView?.Frame.Right - Frame.Right < 0)
  4607. {
  4608. w = SuperView!.Frame.Right - Frame.Right - 1;
  4609. }
  4610. if (SuperView?.Frame.Bottom - Frame.Bottom < 0)
  4611. {
  4612. h = SuperView!.Frame.Bottom - Frame.Bottom - 1;
  4613. }
  4614. return (w, h);
  4615. }
  4616. private bool PointInSelection (int col, int row)
  4617. {
  4618. long start, end;
  4619. GetEncodedRegionBounds (out start, out end);
  4620. long q = ((long)(uint)row << 32) | (uint)col;
  4621. return q >= start && q <= end - 1;
  4622. }
  4623. private void ProcessAutocomplete ()
  4624. {
  4625. if (_isDrawing)
  4626. {
  4627. return;
  4628. }
  4629. if (_clickWithSelecting)
  4630. {
  4631. _clickWithSelecting = false;
  4632. return;
  4633. }
  4634. if (SelectedLength > 0)
  4635. {
  4636. return;
  4637. }
  4638. // draw autocomplete
  4639. GenerateSuggestions ();
  4640. var renderAt = new Point (
  4641. Autocomplete.Context.CursorPosition,
  4642. Autocomplete.PopupInsideContainer
  4643. ? CursorPosition.Y + 1 - TopRow
  4644. : 0
  4645. );
  4646. Autocomplete.RenderOverlay (renderAt);
  4647. }
  4648. private bool ProcessBackTab ()
  4649. {
  4650. ResetColumnTrack ();
  4651. if (!AllowsTab || _isReadOnly)
  4652. {
  4653. return ProcessMovePreviousView ();
  4654. }
  4655. if (CurrentColumn > 0)
  4656. {
  4657. SetWrapModel ();
  4658. List<RuneCell> currentLine = GetCurrentLine ();
  4659. if (currentLine.Count > 0 && currentLine [CurrentColumn - 1].Rune.Value == '\t')
  4660. {
  4661. _historyText.Add (new List<List<RuneCell>> { new (currentLine) }, CursorPosition);
  4662. currentLine.RemoveAt (CurrentColumn - 1);
  4663. CurrentColumn--;
  4664. _historyText.Add (
  4665. new List<List<RuneCell>> { new (GetCurrentLine ()) },
  4666. CursorPosition,
  4667. HistoryText.LineStatus.Replaced
  4668. );
  4669. }
  4670. SetNeedsDisplay ();
  4671. UpdateWrapModel ();
  4672. }
  4673. DoNeededAction ();
  4674. return true;
  4675. }
  4676. private void ProcessCopy ()
  4677. {
  4678. ResetColumnTrack ();
  4679. Copy ();
  4680. }
  4681. private void ProcessCut ()
  4682. {
  4683. ResetColumnTrack ();
  4684. Cut ();
  4685. }
  4686. private void ProcessDeleteCharLeft ()
  4687. {
  4688. ResetColumnTrack ();
  4689. DeleteCharLeft ();
  4690. }
  4691. private void ProcessDeleteCharRight ()
  4692. {
  4693. ResetColumnTrack ();
  4694. DeleteCharRight ();
  4695. }
  4696. // If InheritsPreviousColorScheme is enabled this method will check if the rune cell on
  4697. // the row and col location and around has a not null color scheme. If it's null will set it with
  4698. // the very most previous valid color scheme.
  4699. private void ProcessInheritsPreviousColorScheme (int row, int col)
  4700. {
  4701. if (!InheritsPreviousColorScheme || (Lines == 1 && GetLine (Lines).Count == 0))
  4702. {
  4703. return;
  4704. }
  4705. List<RuneCell> line = GetLine (row);
  4706. List<RuneCell> lineToSet = line;
  4707. while (line.Count == 0)
  4708. {
  4709. if (row == 0 && line.Count == 0)
  4710. {
  4711. return;
  4712. }
  4713. row--;
  4714. line = GetLine (row);
  4715. lineToSet = line;
  4716. }
  4717. int colWithColor = Math.Max (Math.Min (col - 2, line.Count - 1), 0);
  4718. RuneCell cell = line [colWithColor];
  4719. int colWithoutColor = Math.Max (col - 1, 0);
  4720. if (cell.ColorScheme is { } && colWithColor == 0 && lineToSet [colWithoutColor].ColorScheme is { })
  4721. {
  4722. for (int r = row - 1; r > -1; r--)
  4723. {
  4724. List<RuneCell> l = GetLine (r);
  4725. for (int c = l.Count - 1; c > -1; c--)
  4726. {
  4727. if (l [c].ColorScheme is null)
  4728. {
  4729. l [c].ColorScheme = cell.ColorScheme;
  4730. }
  4731. else
  4732. {
  4733. return;
  4734. }
  4735. }
  4736. }
  4737. return;
  4738. }
  4739. if (cell.ColorScheme is null)
  4740. {
  4741. for (int r = row; r > -1; r--)
  4742. {
  4743. List<RuneCell> l = GetLine (r);
  4744. colWithColor = l.FindLastIndex (
  4745. colWithColor > -1 ? colWithColor : l.Count - 1,
  4746. rc => rc.ColorScheme != null
  4747. );
  4748. if (colWithColor > -1 && l [colWithColor].ColorScheme is { })
  4749. {
  4750. cell = l [colWithColor];
  4751. break;
  4752. }
  4753. }
  4754. }
  4755. else
  4756. {
  4757. int cRow = row;
  4758. while (cell.ColorScheme is null)
  4759. {
  4760. if ((colWithColor == 0 || cell.ColorScheme is null) && cRow > 0)
  4761. {
  4762. line = GetLine (--cRow);
  4763. colWithColor = line.Count - 1;
  4764. cell = line [colWithColor];
  4765. }
  4766. else if (cRow == 0 && colWithColor < line.Count)
  4767. {
  4768. cell = line [colWithColor + 1];
  4769. }
  4770. }
  4771. }
  4772. if (cell.ColorScheme is { } && colWithColor > -1 && colWithoutColor < lineToSet.Count && lineToSet [colWithoutColor].ColorScheme is null)
  4773. {
  4774. while (lineToSet [colWithoutColor].ColorScheme is null)
  4775. {
  4776. lineToSet [colWithoutColor].ColorScheme = cell.ColorScheme;
  4777. colWithoutColor--;
  4778. if (colWithoutColor == -1 && row > 0)
  4779. {
  4780. lineToSet = GetLine (--row);
  4781. colWithoutColor = lineToSet.Count - 1;
  4782. }
  4783. }
  4784. }
  4785. }
  4786. private void ProcessKillWordBackward ()
  4787. {
  4788. ResetColumnTrack ();
  4789. KillWordBackward ();
  4790. }
  4791. private void ProcessKillWordForward ()
  4792. {
  4793. ResetColumnTrack ();
  4794. KillWordForward ();
  4795. }
  4796. private void ProcessMouseClick (MouseEvent ev, out List<RuneCell> line)
  4797. {
  4798. List<RuneCell>? r = null;
  4799. if (_model.Count > 0)
  4800. {
  4801. int maxCursorPositionableLine = Math.Max (_model.Count - 1 - _topRow, 0);
  4802. if (Math.Max (ev.Y, 0) > maxCursorPositionableLine)
  4803. {
  4804. CurrentRow = maxCursorPositionableLine + _topRow;
  4805. }
  4806. else
  4807. {
  4808. CurrentRow = Math.Max (ev.Y + _topRow, 0);
  4809. }
  4810. r = GetCurrentLine ();
  4811. int idx = TextModel.GetColFromX (r, _leftColumn, Math.Max (ev.X, 0), TabWidth);
  4812. if (idx - _leftColumn >= r.Count + RightOffset)
  4813. {
  4814. CurrentColumn = Math.Max (r.Count - _leftColumn + RightOffset, 0);
  4815. }
  4816. else
  4817. {
  4818. CurrentColumn = idx + _leftColumn;
  4819. }
  4820. }
  4821. line = r!;
  4822. }
  4823. private void ProcessMoveDown ()
  4824. {
  4825. ResetContinuousFindTrack ();
  4826. if (_shiftSelecting && Selecting)
  4827. {
  4828. StopSelecting ();
  4829. }
  4830. MoveDown ();
  4831. }
  4832. private void ProcessMoveDownExtend ()
  4833. {
  4834. ResetColumnTrack ();
  4835. StartSelecting ();
  4836. MoveDown ();
  4837. }
  4838. private void ProcessMoveEndOfLine ()
  4839. {
  4840. ResetAllTrack ();
  4841. if (_shiftSelecting && Selecting)
  4842. {
  4843. StopSelecting ();
  4844. }
  4845. MoveEndOfLine ();
  4846. }
  4847. private void ProcessMoveEndOfLineExtend ()
  4848. {
  4849. ResetAllTrack ();
  4850. StartSelecting ();
  4851. MoveEndOfLine ();
  4852. }
  4853. private bool ProcessMoveLeft ()
  4854. {
  4855. // if the user presses Left (without any control keys) and they are at the start of the text
  4856. if (CurrentColumn == 0 && CurrentRow == 0)
  4857. {
  4858. // do not respond (this lets the key press fall through to navigation system - which usually changes focus backward)
  4859. return false;
  4860. }
  4861. ResetAllTrack ();
  4862. if (_shiftSelecting && Selecting)
  4863. {
  4864. StopSelecting ();
  4865. }
  4866. MoveLeft ();
  4867. return true;
  4868. }
  4869. private void ProcessMoveLeftExtend ()
  4870. {
  4871. ResetAllTrack ();
  4872. StartSelecting ();
  4873. MoveLeft ();
  4874. }
  4875. private bool ProcessMoveNextView ()
  4876. {
  4877. ResetColumnTrack ();
  4878. return MoveNextView ();
  4879. }
  4880. private bool ProcessMovePreviousView ()
  4881. {
  4882. ResetColumnTrack ();
  4883. return MovePreviousView ();
  4884. }
  4885. private bool ProcessMoveRight ()
  4886. {
  4887. // if the user presses Right (without any control keys)
  4888. // determine where the last cursor position in the text is
  4889. int lastRow = _model.Count - 1;
  4890. int lastCol = _model.GetLine (lastRow).Count;
  4891. // if they are at the very end of all the text do not respond (this lets the key press fall through to navigation system - which usually changes focus forward)
  4892. if (CurrentColumn == lastCol && CurrentRow == lastRow)
  4893. {
  4894. return false;
  4895. }
  4896. ResetAllTrack ();
  4897. if (_shiftSelecting && Selecting)
  4898. {
  4899. StopSelecting ();
  4900. }
  4901. MoveRight ();
  4902. return true;
  4903. }
  4904. private void ProcessMoveRightExtend ()
  4905. {
  4906. ResetAllTrack ();
  4907. StartSelecting ();
  4908. MoveRight ();
  4909. }
  4910. private void ProcessMoveStartOfLine ()
  4911. {
  4912. ResetAllTrack ();
  4913. if (_shiftSelecting && Selecting)
  4914. {
  4915. StopSelecting ();
  4916. }
  4917. MoveStartOfLine ();
  4918. }
  4919. private void ProcessMoveStartOfLineExtend ()
  4920. {
  4921. ResetAllTrack ();
  4922. StartSelecting ();
  4923. MoveStartOfLine ();
  4924. }
  4925. private void ProcessMoveUp ()
  4926. {
  4927. ResetContinuousFindTrack ();
  4928. if (_shiftSelecting && Selecting)
  4929. {
  4930. StopSelecting ();
  4931. }
  4932. MoveUp ();
  4933. }
  4934. private void ProcessMoveUpExtend ()
  4935. {
  4936. ResetColumnTrack ();
  4937. StartSelecting ();
  4938. MoveUp ();
  4939. }
  4940. private void ProcessMoveWordBackward ()
  4941. {
  4942. ResetAllTrack ();
  4943. if (_shiftSelecting && Selecting)
  4944. {
  4945. StopSelecting ();
  4946. }
  4947. MoveWordBackward ();
  4948. }
  4949. private void ProcessMoveWordBackwardExtend ()
  4950. {
  4951. ResetAllTrack ();
  4952. StartSelecting ();
  4953. MoveWordBackward ();
  4954. }
  4955. private void ProcessMoveWordForward ()
  4956. {
  4957. ResetAllTrack ();
  4958. if (_shiftSelecting && Selecting)
  4959. {
  4960. StopSelecting ();
  4961. }
  4962. MoveWordForward ();
  4963. }
  4964. private void ProcessMoveWordForwardExtend ()
  4965. {
  4966. ResetAllTrack ();
  4967. StartSelecting ();
  4968. MoveWordForward ();
  4969. }
  4970. private void ProcessPageDown ()
  4971. {
  4972. ResetColumnTrack ();
  4973. if (_shiftSelecting && Selecting)
  4974. {
  4975. StopSelecting ();
  4976. }
  4977. MovePageDown ();
  4978. }
  4979. private void ProcessPageDownExtend ()
  4980. {
  4981. ResetColumnTrack ();
  4982. StartSelecting ();
  4983. MovePageDown ();
  4984. }
  4985. private void ProcessPageUp ()
  4986. {
  4987. ResetColumnTrack ();
  4988. if (_shiftSelecting && Selecting)
  4989. {
  4990. StopSelecting ();
  4991. }
  4992. MovePageUp ();
  4993. }
  4994. private void ProcessPageUpExtend ()
  4995. {
  4996. ResetColumnTrack ();
  4997. StartSelecting ();
  4998. MovePageUp ();
  4999. }
  5000. private void ProcessPaste ()
  5001. {
  5002. ResetColumnTrack ();
  5003. if (_isReadOnly)
  5004. {
  5005. return;
  5006. }
  5007. Paste ();
  5008. }
  5009. private bool ProcessReturn ()
  5010. {
  5011. ResetColumnTrack ();
  5012. if (!AllowsReturn || _isReadOnly)
  5013. {
  5014. return false;
  5015. }
  5016. SetWrapModel ();
  5017. List<RuneCell> currentLine = GetCurrentLine ();
  5018. _historyText.Add (new List<List<RuneCell>> { new (currentLine) }, CursorPosition);
  5019. if (Selecting)
  5020. {
  5021. ClearSelectedRegion ();
  5022. currentLine = GetCurrentLine ();
  5023. }
  5024. int restCount = currentLine.Count - CurrentColumn;
  5025. List<RuneCell> rest = currentLine.GetRange (CurrentColumn, restCount);
  5026. currentLine.RemoveRange (CurrentColumn, restCount);
  5027. List<List<RuneCell>> addedLines = new () { new List<RuneCell> (currentLine) };
  5028. _model.AddLine (CurrentRow + 1, rest);
  5029. addedLines.Add (new List<RuneCell> (_model.GetLine (CurrentRow + 1)));
  5030. _historyText.Add (addedLines, CursorPosition, HistoryText.LineStatus.Added);
  5031. CurrentRow++;
  5032. var fullNeedsDisplay = false;
  5033. if (CurrentRow >= _topRow + Frame.Height)
  5034. {
  5035. _topRow++;
  5036. fullNeedsDisplay = true;
  5037. }
  5038. CurrentColumn = 0;
  5039. _historyText.Add (
  5040. new List<List<RuneCell>> { new (GetCurrentLine ()) },
  5041. CursorPosition,
  5042. HistoryText.LineStatus.Replaced
  5043. );
  5044. if (!_wordWrap && CurrentColumn < _leftColumn)
  5045. {
  5046. fullNeedsDisplay = true;
  5047. _leftColumn = 0;
  5048. }
  5049. if (fullNeedsDisplay)
  5050. {
  5051. SetNeedsDisplay ();
  5052. }
  5053. else
  5054. {
  5055. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  5056. //SetNeedsDisplay (new (0, currentRow - topRow, 2, Frame.Height));
  5057. SetNeedsDisplay ();
  5058. }
  5059. UpdateWrapModel ();
  5060. DoNeededAction ();
  5061. OnContentsChanged ();
  5062. return true;
  5063. }
  5064. private void ProcessSelectAll ()
  5065. {
  5066. ResetColumnTrack ();
  5067. SelectAll ();
  5068. }
  5069. private void ProcessSetOverwrite ()
  5070. {
  5071. ResetColumnTrack ();
  5072. SetOverwrite (!Used);
  5073. }
  5074. private bool ProcessTab ()
  5075. {
  5076. ResetColumnTrack ();
  5077. if (!AllowsTab || _isReadOnly)
  5078. {
  5079. return ProcessMoveNextView ();
  5080. }
  5081. InsertText (new Key ((KeyCode)'\t'));
  5082. DoNeededAction ();
  5083. return true;
  5084. }
  5085. private void ResetAllTrack ()
  5086. {
  5087. // Handle some state here - whether the last command was a kill
  5088. // operation and the column tracking (up/down)
  5089. _lastWasKill = false;
  5090. _columnTrack = -1;
  5091. _continuousFind = false;
  5092. }
  5093. private void ResetColumnTrack ()
  5094. {
  5095. // Handle some state here - whether the last command was a kill
  5096. // operation and the column tracking (up/down)
  5097. _lastWasKill = false;
  5098. _columnTrack = -1;
  5099. }
  5100. private void ResetContinuousFind ()
  5101. {
  5102. if (!_continuousFind)
  5103. {
  5104. int col = Selecting ? _selectionStartColumn : CurrentColumn;
  5105. int row = Selecting ? _selectionStartRow : CurrentRow;
  5106. _model.ResetContinuousFind (new Point (col, row));
  5107. }
  5108. }
  5109. private void ResetContinuousFindTrack ()
  5110. {
  5111. // Handle some state here - whether the last command was a kill
  5112. // operation and the column tracking (up/down)
  5113. _lastWasKill = false;
  5114. _continuousFind = false;
  5115. }
  5116. private void ResetCursorVisibility ()
  5117. {
  5118. if (_savedCursorVisibility != 0)
  5119. {
  5120. DesiredCursorVisibility = _savedCursorVisibility;
  5121. _savedCursorVisibility = 0;
  5122. }
  5123. }
  5124. private void ResetPosition ()
  5125. {
  5126. _topRow = _leftColumn = CurrentRow = CurrentColumn = 0;
  5127. StopSelecting ();
  5128. ResetCursorVisibility ();
  5129. }
  5130. private void SaveCursorVisibility ()
  5131. {
  5132. if (_desiredCursorVisibility != CursorVisibility.Invisible)
  5133. {
  5134. if (_savedCursorVisibility == 0)
  5135. {
  5136. _savedCursorVisibility = _desiredCursorVisibility;
  5137. }
  5138. DesiredCursorVisibility = CursorVisibility.Invisible;
  5139. }
  5140. }
  5141. private void SetClipboard (string text)
  5142. {
  5143. if (text is { })
  5144. {
  5145. Clipboard.Contents = text;
  5146. }
  5147. }
  5148. private bool SetFoundText (
  5149. string text,
  5150. (Point current, bool found) foundPos,
  5151. string? textToReplace = null,
  5152. bool replace = false,
  5153. bool replaceAll = false
  5154. )
  5155. {
  5156. if (foundPos.found)
  5157. {
  5158. StartSelecting ();
  5159. _selectionStartColumn = foundPos.current.X;
  5160. _selectionStartRow = foundPos.current.Y;
  5161. if (!replaceAll)
  5162. {
  5163. CurrentColumn = _selectionStartColumn + text.GetRuneCount ();
  5164. }
  5165. else
  5166. {
  5167. CurrentColumn = _selectionStartColumn + textToReplace!.GetRuneCount ();
  5168. }
  5169. CurrentRow = foundPos.current.Y;
  5170. if (!_isReadOnly && replace)
  5171. {
  5172. Adjust ();
  5173. ClearSelectedRegion ();
  5174. InsertAllText (textToReplace!);
  5175. StartSelecting ();
  5176. _selectionStartColumn = CurrentColumn - textToReplace!.GetRuneCount ();
  5177. }
  5178. else
  5179. {
  5180. UpdateWrapModel ();
  5181. SetNeedsDisplay ();
  5182. Adjust ();
  5183. }
  5184. _continuousFind = true;
  5185. return foundPos.found;
  5186. }
  5187. UpdateWrapModel ();
  5188. _continuousFind = false;
  5189. return foundPos.found;
  5190. }
  5191. private void SetOverwrite (bool overwrite)
  5192. {
  5193. Used = overwrite;
  5194. SetNeedsDisplay ();
  5195. DoNeededAction ();
  5196. }
  5197. private static void SetValidUsedColor (ColorScheme colorScheme)
  5198. {
  5199. // BUGBUG: (v2 truecolor) This code depends on 8-bit color names; disabling for now
  5200. //if ((colorScheme!.HotNormal.Foreground & colorScheme.Focus.Background) == colorScheme.Focus.Foreground) {
  5201. Driver.SetAttribute (new Attribute (colorScheme.Focus.Background, colorScheme.Focus.Foreground));
  5202. }
  5203. /// <summary>Restore from original model.</summary>
  5204. private void SetWrapModel ([CallerMemberName] string? caller = null)
  5205. {
  5206. if (_currentCaller is { })
  5207. {
  5208. return;
  5209. }
  5210. if (_wordWrap)
  5211. {
  5212. _currentCaller = caller;
  5213. CurrentColumn = _wrapManager!.GetModelColFromWrappedLines (CurrentRow, CurrentColumn);
  5214. CurrentRow = _wrapManager.GetModelLineFromWrappedLines (CurrentRow);
  5215. _selectionStartColumn =
  5216. _wrapManager.GetModelColFromWrappedLines (_selectionStartRow, _selectionStartColumn);
  5217. _selectionStartRow = _wrapManager.GetModelLineFromWrappedLines (_selectionStartRow);
  5218. _model = _wrapManager.Model;
  5219. }
  5220. }
  5221. private void ShowContextMenu ()
  5222. {
  5223. if (_currentCulture != Thread.CurrentThread.CurrentUICulture)
  5224. {
  5225. _currentCulture = Thread.CurrentThread.CurrentUICulture;
  5226. ContextMenu!.MenuItems = BuildContextMenuBarItem ();
  5227. }
  5228. ContextMenu!.Show ();
  5229. }
  5230. private void StartSelecting ()
  5231. {
  5232. if (_shiftSelecting && Selecting)
  5233. {
  5234. return;
  5235. }
  5236. _shiftSelecting = true;
  5237. Selecting = true;
  5238. _selectionStartColumn = CurrentColumn;
  5239. _selectionStartRow = CurrentRow;
  5240. }
  5241. private void StopSelecting ()
  5242. {
  5243. _shiftSelecting = false;
  5244. Selecting = false;
  5245. _isButtonShift = false;
  5246. }
  5247. private string StringFromRunes (List<RuneCell> cells)
  5248. {
  5249. if (cells is null)
  5250. {
  5251. throw new ArgumentNullException (nameof (cells));
  5252. }
  5253. var size = 0;
  5254. foreach (RuneCell cell in cells)
  5255. {
  5256. size += cell.Rune.GetEncodingLength ();
  5257. }
  5258. var encoded = new byte [size];
  5259. var offset = 0;
  5260. foreach (RuneCell cell in cells)
  5261. {
  5262. offset += cell.Rune.Encode (encoded, offset);
  5263. }
  5264. return StringExtensions.ToString (encoded);
  5265. }
  5266. private void TextView_Initialized (object sender, EventArgs e)
  5267. {
  5268. Autocomplete.HostControl = this;
  5269. if (Application.Top is { })
  5270. {
  5271. Application.Top.AlternateForwardKeyChanged += Top_AlternateForwardKeyChanged!;
  5272. Application.Top.AlternateBackwardKeyChanged += Top_AlternateBackwardKeyChanged!;
  5273. }
  5274. OnContentsChanged ();
  5275. }
  5276. private void TextView_LayoutComplete (object? sender, LayoutEventArgs e)
  5277. {
  5278. WrapTextModel ();
  5279. Adjust ();
  5280. }
  5281. private void ToggleSelecting ()
  5282. {
  5283. ResetColumnTrack ();
  5284. Selecting = !Selecting;
  5285. _selectionStartColumn = CurrentColumn;
  5286. _selectionStartRow = CurrentRow;
  5287. }
  5288. private void Top_AlternateBackwardKeyChanged (object sender, KeyChangedEventArgs e) { KeyBindings.Replace (e.OldKey, e.NewKey); }
  5289. private void Top_AlternateForwardKeyChanged (object sender, KeyChangedEventArgs e) { KeyBindings.Replace (e.OldKey, e.NewKey); }
  5290. // Tries to snap the cursor to the tracking column
  5291. private void TrackColumn ()
  5292. {
  5293. // Now track the column
  5294. List<RuneCell> line = GetCurrentLine ();
  5295. if (line.Count < _columnTrack)
  5296. {
  5297. CurrentColumn = line.Count;
  5298. }
  5299. else if (_columnTrack != -1)
  5300. {
  5301. CurrentColumn = _columnTrack;
  5302. }
  5303. else if (CurrentColumn > line.Count)
  5304. {
  5305. CurrentColumn = line.Count;
  5306. }
  5307. Adjust ();
  5308. }
  5309. /// <summary>Update the original model.</summary>
  5310. private void UpdateWrapModel ([CallerMemberName] string? caller = null)
  5311. {
  5312. if (_currentCaller is { } && _currentCaller != caller)
  5313. {
  5314. return;
  5315. }
  5316. if (_wordWrap)
  5317. {
  5318. _currentCaller = null;
  5319. _wrapManager!.UpdateModel (
  5320. _model,
  5321. out int nRow,
  5322. out int nCol,
  5323. out int nStartRow,
  5324. out int nStartCol,
  5325. CurrentRow,
  5326. CurrentColumn,
  5327. _selectionStartRow,
  5328. _selectionStartColumn,
  5329. true
  5330. );
  5331. CurrentRow = nRow;
  5332. CurrentColumn = nCol;
  5333. _selectionStartRow = nStartRow;
  5334. _selectionStartColumn = nStartCol;
  5335. _wrapNeeded = true;
  5336. SetNeedsDisplay ();
  5337. }
  5338. if (_currentCaller is { })
  5339. {
  5340. throw new InvalidOperationException (
  5341. $"WordWrap settings was changed after the {_currentCaller} call."
  5342. );
  5343. }
  5344. }
  5345. private void WrapTextModel ()
  5346. {
  5347. if (_wordWrap && _wrapManager is { })
  5348. {
  5349. _model = _wrapManager.WrapModel (
  5350. _frameWidth,
  5351. out int nRow,
  5352. out int nCol,
  5353. out int nStartRow,
  5354. out int nStartCol,
  5355. CurrentRow,
  5356. CurrentColumn,
  5357. _selectionStartRow,
  5358. _selectionStartColumn,
  5359. _tabWidth
  5360. );
  5361. CurrentRow = nRow;
  5362. CurrentColumn = nCol;
  5363. _selectionStartRow = nStartRow;
  5364. _selectionStartColumn = nStartCol;
  5365. SetNeedsDisplay ();
  5366. }
  5367. }
  5368. }
  5369. /// <summary>
  5370. /// Renders an overlay on another view at a given point that allows selecting from a range of 'autocomplete'
  5371. /// options. An implementation on a TextView.
  5372. /// </summary>
  5373. public class TextViewAutocomplete : PopupAutocomplete
  5374. {
  5375. /// <inheritdoc/>
  5376. protected override void DeleteTextBackwards () { ((TextView)HostControl).DeleteCharLeft (); }
  5377. /// <inheritdoc/>
  5378. protected override void InsertText (string accepted) { ((TextView)HostControl).InsertText (accepted); }
  5379. /// <inheritdoc/>
  5380. protected override void SetCursorPosition (int column)
  5381. {
  5382. ((TextView)HostControl).CursorPosition =
  5383. new Point (column, ((TextView)HostControl).CurrentRow);
  5384. }
  5385. }