TextView.cs 135 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801
  1. using System.Globalization;
  2. using System.Runtime.CompilerServices;
  3. namespace Terminal.Gui.Views;
  4. /// <summary>Fully featured multi-line text editor</summary>
  5. /// <remarks>
  6. /// <list type="table">
  7. /// <listheader>
  8. /// <term>Shortcut</term> <description>Action performed</description>
  9. /// </listheader>
  10. /// <item>
  11. /// <term>Left cursor, Control-b</term> <description>Moves the editing point left.</description>
  12. /// </item>
  13. /// <item>
  14. /// <term>Right cursor, Control-f</term> <description>Moves the editing point right.</description>
  15. /// </item>
  16. /// <item>
  17. /// <term>Alt-b</term> <description>Moves one word back.</description>
  18. /// </item>
  19. /// <item>
  20. /// <term>Alt-f</term> <description>Moves one word forward.</description>
  21. /// </item>
  22. /// <item>
  23. /// <term>Up cursor, Control-p</term> <description>Moves the editing point one line up.</description>
  24. /// </item>
  25. /// <item>
  26. /// <term>Down cursor, Control-n</term> <description>Moves the editing point one line down</description>
  27. /// </item>
  28. /// <item>
  29. /// <term>Home key, Control-a</term> <description>Moves the cursor to the beginning of the line.</description>
  30. /// </item>
  31. /// <item>
  32. /// <term>End key, Control-e</term> <description>Moves the cursor to the end of the line.</description>
  33. /// </item>
  34. /// <item>
  35. /// <term>Control-Home</term> <description>Scrolls to the first line and moves the cursor there.</description>
  36. /// </item>
  37. /// <item>
  38. /// <term>Control-End</term> <description>Scrolls to the last line and moves the cursor there.</description>
  39. /// </item>
  40. /// <item>
  41. /// <term>Delete, Control-d</term> <description>Deletes the character in front of the cursor.</description>
  42. /// </item>
  43. /// <item>
  44. /// <term>Backspace</term> <description>Deletes the character behind the cursor.</description>
  45. /// </item>
  46. /// <item>
  47. /// <term>Control-k</term>
  48. /// <description>
  49. /// Deletes the text until the end of the line and replaces the kill buffer with the deleted text.
  50. /// You can paste this text in a different place by using Control-y.
  51. /// </description>
  52. /// </item>
  53. /// <item>
  54. /// <term>Control-y</term>
  55. /// <description>Pastes the content of the kill ring into the current position.</description>
  56. /// </item>
  57. /// <item>
  58. /// <term>Alt-d</term>
  59. /// <description>
  60. /// Deletes the word above the cursor and adds it to the kill ring. You can paste the contents of
  61. /// the kill ring with Control-y.
  62. /// </description>
  63. /// </item>
  64. /// <item>
  65. /// <term>Control-q</term>
  66. /// <description>
  67. /// Quotes the next input character, to prevent the normal processing of key handling to take
  68. /// place.
  69. /// </description>
  70. /// </item>
  71. /// </list>
  72. /// </remarks>
  73. public class TextView : View, IDesignable
  74. {
  75. private readonly HistoryText _historyText = new ();
  76. private bool _allowsReturn = true;
  77. private bool _allowsTab = true;
  78. private bool _clickWithSelecting;
  79. // The column we are tracking, or -1 if we are not tracking any column
  80. private int _columnTrack = -1;
  81. private bool _continuousFind;
  82. private bool _copyWithoutSelection;
  83. private string? _currentCaller;
  84. private CultureInfo? _currentCulture;
  85. private bool _isButtonShift;
  86. private bool _isButtonReleased;
  87. private bool _isDrawing;
  88. private bool _isReadOnly;
  89. private bool _lastWasKill;
  90. private int _leftColumn;
  91. private TextModel _model = new ();
  92. private bool _multiline = true;
  93. private Dim? _savedHeight;
  94. private int _selectionStartColumn, _selectionStartRow;
  95. private bool _shiftSelecting;
  96. private int _tabWidth = 4;
  97. private int _topRow;
  98. private bool _wordWrap;
  99. private WordWrapManager? _wrapManager;
  100. private bool _wrapNeeded;
  101. /// <summary>
  102. /// Initializes a <see cref="TextView"/> on the specified area, with dimensions controlled with the X, Y, Width
  103. /// and Height properties.
  104. /// </summary>
  105. public TextView ()
  106. {
  107. CanFocus = true;
  108. CursorVisibility = CursorVisibility.Default;
  109. Used = true;
  110. // By default, disable hotkeys (in case someone sets Title)
  111. HotKeySpecifier = new ('\xffff');
  112. _model.LinesLoaded += Model_LinesLoaded!;
  113. _historyText.ChangeText += HistoryText_ChangeText!;
  114. Initialized += TextView_Initialized!;
  115. SuperViewChanged += TextView_SuperViewChanged!;
  116. SubViewsLaidOut += TextView_LayoutComplete;
  117. // Things this view knows how to do
  118. // Note - NewLine is only bound to Enter if Multiline is true
  119. AddCommand (Command.NewLine, ctx => ProcessEnterKey (ctx));
  120. AddCommand (
  121. Command.PageDown,
  122. () =>
  123. {
  124. ProcessPageDown ();
  125. return true;
  126. }
  127. );
  128. AddCommand (
  129. Command.PageDownExtend,
  130. () =>
  131. {
  132. ProcessPageDownExtend ();
  133. return true;
  134. }
  135. );
  136. AddCommand (
  137. Command.PageUp,
  138. () =>
  139. {
  140. ProcessPageUp ();
  141. return true;
  142. }
  143. );
  144. AddCommand (
  145. Command.PageUpExtend,
  146. () =>
  147. {
  148. ProcessPageUpExtend ();
  149. return true;
  150. }
  151. );
  152. AddCommand (Command.Down, () => ProcessMoveDown ());
  153. AddCommand (
  154. Command.DownExtend,
  155. () =>
  156. {
  157. ProcessMoveDownExtend ();
  158. return true;
  159. }
  160. );
  161. AddCommand (Command.Up, () => ProcessMoveUp ());
  162. AddCommand (
  163. Command.UpExtend,
  164. () =>
  165. {
  166. ProcessMoveUpExtend ();
  167. return true;
  168. }
  169. );
  170. AddCommand (Command.Right, () => ProcessMoveRight ());
  171. AddCommand (
  172. Command.RightExtend,
  173. () =>
  174. {
  175. ProcessMoveRightExtend ();
  176. return true;
  177. }
  178. );
  179. AddCommand (Command.Left, () => ProcessMoveLeft ());
  180. AddCommand (
  181. Command.LeftExtend,
  182. () =>
  183. {
  184. ProcessMoveLeftExtend ();
  185. return true;
  186. }
  187. );
  188. AddCommand (
  189. Command.DeleteCharLeft,
  190. () =>
  191. {
  192. ProcessDeleteCharLeft ();
  193. return true;
  194. }
  195. );
  196. AddCommand (
  197. Command.LeftStart,
  198. () =>
  199. {
  200. ProcessMoveLeftStart ();
  201. return true;
  202. }
  203. );
  204. AddCommand (
  205. Command.LeftStartExtend,
  206. () =>
  207. {
  208. ProcessMoveLeftStartExtend ();
  209. return true;
  210. }
  211. );
  212. AddCommand (
  213. Command.DeleteCharRight,
  214. () =>
  215. {
  216. ProcessDeleteCharRight ();
  217. return true;
  218. }
  219. );
  220. AddCommand (
  221. Command.RightEnd,
  222. () =>
  223. {
  224. ProcessMoveEndOfLine ();
  225. return true;
  226. }
  227. );
  228. AddCommand (
  229. Command.RightEndExtend,
  230. () =>
  231. {
  232. ProcessMoveRightEndExtend ();
  233. return true;
  234. }
  235. );
  236. AddCommand (
  237. Command.CutToEndLine,
  238. () =>
  239. {
  240. KillToEndOfLine ();
  241. return true;
  242. }
  243. );
  244. AddCommand (
  245. Command.CutToStartLine,
  246. () =>
  247. {
  248. KillToLeftStart ();
  249. return true;
  250. }
  251. );
  252. AddCommand (
  253. Command.Paste,
  254. () =>
  255. {
  256. ProcessPaste ();
  257. return true;
  258. }
  259. );
  260. AddCommand (
  261. Command.ToggleExtend,
  262. () =>
  263. {
  264. ToggleSelecting ();
  265. return true;
  266. }
  267. );
  268. AddCommand (
  269. Command.Copy,
  270. () =>
  271. {
  272. ProcessCopy ();
  273. return true;
  274. }
  275. );
  276. AddCommand (
  277. Command.Cut,
  278. () =>
  279. {
  280. ProcessCut ();
  281. return true;
  282. }
  283. );
  284. AddCommand (
  285. Command.WordLeft,
  286. () =>
  287. {
  288. ProcessMoveWordBackward ();
  289. return true;
  290. }
  291. );
  292. AddCommand (
  293. Command.WordLeftExtend,
  294. () =>
  295. {
  296. ProcessMoveWordBackwardExtend ();
  297. return true;
  298. }
  299. );
  300. AddCommand (
  301. Command.WordRight,
  302. () =>
  303. {
  304. ProcessMoveWordForward ();
  305. return true;
  306. }
  307. );
  308. AddCommand (
  309. Command.WordRightExtend,
  310. () =>
  311. {
  312. ProcessMoveWordForwardExtend ();
  313. return true;
  314. }
  315. );
  316. AddCommand (
  317. Command.KillWordForwards,
  318. () =>
  319. {
  320. ProcessKillWordForward ();
  321. return true;
  322. }
  323. );
  324. AddCommand (
  325. Command.KillWordBackwards,
  326. () =>
  327. {
  328. ProcessKillWordBackward ();
  329. return true;
  330. }
  331. );
  332. AddCommand (
  333. Command.End,
  334. () =>
  335. {
  336. MoveBottomEnd ();
  337. return true;
  338. }
  339. );
  340. AddCommand (
  341. Command.EndExtend,
  342. () =>
  343. {
  344. MoveBottomEndExtend ();
  345. return true;
  346. }
  347. );
  348. AddCommand (
  349. Command.Start,
  350. () =>
  351. {
  352. MoveTopHome ();
  353. return true;
  354. }
  355. );
  356. AddCommand (
  357. Command.StartExtend,
  358. () =>
  359. {
  360. MoveTopHomeExtend ();
  361. return true;
  362. }
  363. );
  364. AddCommand (
  365. Command.SelectAll,
  366. () =>
  367. {
  368. ProcessSelectAll ();
  369. return true;
  370. }
  371. );
  372. AddCommand (
  373. Command.ToggleOverwrite,
  374. () =>
  375. {
  376. ProcessSetOverwrite ();
  377. return true;
  378. }
  379. );
  380. AddCommand (
  381. Command.EnableOverwrite,
  382. () =>
  383. {
  384. SetOverwrite (true);
  385. return true;
  386. }
  387. );
  388. AddCommand (
  389. Command.DisableOverwrite,
  390. () =>
  391. {
  392. SetOverwrite (false);
  393. return true;
  394. }
  395. );
  396. AddCommand (Command.Tab, () => ProcessTab ());
  397. AddCommand (Command.BackTab, () => ProcessBackTab ());
  398. AddCommand (
  399. Command.Undo,
  400. () =>
  401. {
  402. Undo ();
  403. return true;
  404. }
  405. );
  406. AddCommand (
  407. Command.Redo,
  408. () =>
  409. {
  410. Redo ();
  411. return true;
  412. }
  413. );
  414. AddCommand (
  415. Command.DeleteAll,
  416. () =>
  417. {
  418. DeleteAll ();
  419. return true;
  420. }
  421. );
  422. AddCommand (
  423. Command.Context,
  424. () =>
  425. {
  426. ShowContextMenu (null);
  427. return true;
  428. }
  429. );
  430. AddCommand (
  431. Command.Open,
  432. () =>
  433. {
  434. PromptForColors ();
  435. return true;
  436. });
  437. // Default keybindings for this view
  438. KeyBindings.Remove (Key.Space);
  439. KeyBindings.Remove (Key.Enter);
  440. KeyBindings.Add (Key.Enter, Multiline ? Command.NewLine : Command.Accept);
  441. KeyBindings.Add (Key.PageDown, Command.PageDown);
  442. KeyBindings.Add (Key.V.WithCtrl, Command.PageDown);
  443. KeyBindings.Add (Key.PageDown.WithShift, Command.PageDownExtend);
  444. KeyBindings.Add (Key.PageUp, Command.PageUp);
  445. KeyBindings.Add (Key.PageUp.WithShift, Command.PageUpExtend);
  446. KeyBindings.Add (Key.N.WithCtrl, Command.Down);
  447. KeyBindings.Add (Key.CursorDown, Command.Down);
  448. KeyBindings.Add (Key.CursorDown.WithShift, Command.DownExtend);
  449. KeyBindings.Add (Key.P.WithCtrl, Command.Up);
  450. KeyBindings.Add (Key.CursorUp, Command.Up);
  451. KeyBindings.Add (Key.CursorUp.WithShift, Command.UpExtend);
  452. KeyBindings.Add (Key.F.WithCtrl, Command.Right);
  453. KeyBindings.Add (Key.CursorRight, Command.Right);
  454. KeyBindings.Add (Key.CursorRight.WithShift, Command.RightExtend);
  455. KeyBindings.Add (Key.B.WithCtrl, Command.Left);
  456. KeyBindings.Add (Key.CursorLeft, Command.Left);
  457. KeyBindings.Add (Key.CursorLeft.WithShift, Command.LeftExtend);
  458. KeyBindings.Add (Key.Backspace, Command.DeleteCharLeft);
  459. KeyBindings.Add (Key.Home, Command.LeftStart);
  460. KeyBindings.Add (Key.Home.WithShift, Command.LeftStartExtend);
  461. KeyBindings.Add (Key.Delete, Command.DeleteCharRight);
  462. KeyBindings.Add (Key.D.WithCtrl, Command.DeleteCharRight);
  463. KeyBindings.Add (Key.End, Command.RightEnd);
  464. KeyBindings.Add (Key.E.WithCtrl, Command.RightEnd);
  465. KeyBindings.Add (Key.End.WithShift, Command.RightEndExtend);
  466. KeyBindings.Add (Key.K.WithCtrl, Command.CutToEndLine); // kill-to-end
  467. KeyBindings.Add (Key.Delete.WithCtrl.WithShift, Command.CutToEndLine); // kill-to-end
  468. KeyBindings.Add (Key.Backspace.WithCtrl.WithShift, Command.CutToStartLine); // kill-to-start
  469. KeyBindings.Add (Key.Y.WithCtrl, Command.Paste); // Control-y, yank
  470. KeyBindings.Add (Key.Space.WithCtrl, Command.ToggleExtend);
  471. KeyBindings.Add (Key.C.WithCtrl, Command.Copy);
  472. KeyBindings.Add (Key.W.WithCtrl, Command.Cut); // Move to Unix?
  473. KeyBindings.Add (Key.X.WithCtrl, Command.Cut);
  474. KeyBindings.Add (Key.CursorLeft.WithCtrl, Command.WordLeft);
  475. KeyBindings.Add (Key.CursorLeft.WithCtrl.WithShift, Command.WordLeftExtend);
  476. KeyBindings.Add (Key.CursorRight.WithCtrl, Command.WordRight);
  477. KeyBindings.Add (Key.CursorRight.WithCtrl.WithShift, Command.WordRightExtend);
  478. KeyBindings.Add (Key.Delete.WithCtrl, Command.KillWordForwards); // kill-word-forwards
  479. KeyBindings.Add (Key.Backspace.WithCtrl, Command.KillWordBackwards); // kill-word-backwards
  480. KeyBindings.Add (Key.End.WithCtrl, Command.End);
  481. KeyBindings.Add (Key.End.WithCtrl.WithShift, Command.EndExtend);
  482. KeyBindings.Add (Key.Home.WithCtrl, Command.Start);
  483. KeyBindings.Add (Key.Home.WithCtrl.WithShift, Command.StartExtend);
  484. KeyBindings.Add (Key.A.WithCtrl, Command.SelectAll);
  485. KeyBindings.Add (Key.InsertChar, Command.ToggleOverwrite);
  486. KeyBindings.Add (Key.Tab, Command.Tab);
  487. KeyBindings.Add (Key.Tab.WithShift, Command.BackTab);
  488. KeyBindings.Add (Key.Z.WithCtrl, Command.Undo);
  489. KeyBindings.Add (Key.R.WithCtrl, Command.Redo);
  490. KeyBindings.Add (Key.G.WithCtrl, Command.DeleteAll);
  491. KeyBindings.Add (Key.D.WithCtrl.WithShift, Command.DeleteAll);
  492. KeyBindings.Add (Key.L.WithCtrl, Command.Open);
  493. #if UNIX_KEY_BINDINGS
  494. KeyBindings.Add (Key.C.WithAlt, Command.Copy);
  495. KeyBindings.Add (Key.B.WithAlt, Command.WordLeft);
  496. KeyBindings.Add (Key.W.WithAlt, Command.Cut);
  497. KeyBindings.Add (Key.V.WithAlt, Command.PageUp);
  498. KeyBindings.Add (Key.F.WithAlt, Command.WordRight);
  499. KeyBindings.Add (Key.K.WithAlt, Command.CutToStartLine); // kill-to-start
  500. #endif
  501. _currentCulture = Thread.CurrentThread.CurrentUICulture;
  502. ContextMenu = CreateContextMenu ();
  503. KeyBindings.Add (ContextMenu.Key, Command.Context);
  504. }
  505. // BUGBUG: AllowsReturn is mis-named. It should be EnterKeyAccepts.
  506. /// <summary>
  507. /// Gets or sets whether pressing ENTER in a <see cref="TextView"/> creates a new line of text
  508. /// in the view or invokes the <see cref="View.Accepting"/> event.
  509. /// </summary>
  510. /// <remarks>
  511. /// <para>
  512. /// Setting this property alters <see cref="Multiline"/>.
  513. /// If <see cref="AllowsReturn"/> is set to <see langword="true"/>, then <see cref="Multiline"/> is also set to
  514. /// `true` and
  515. /// vice-versa.
  516. /// </para>
  517. /// <para>
  518. /// If <see cref="AllowsReturn"/> is set to <see langword="false"/>, then <see cref="AllowsTab"/> gets set to
  519. /// <see langword="false"/>.
  520. /// </para>
  521. /// </remarks>
  522. public bool AllowsReturn
  523. {
  524. get => _allowsReturn;
  525. set
  526. {
  527. _allowsReturn = value;
  528. if (_allowsReturn && !_multiline)
  529. {
  530. // BUGBUG: Setting properties should not have side-effects like this. Multiline and AllowsReturn should be independent.
  531. Multiline = true;
  532. }
  533. if (!_allowsReturn && _multiline)
  534. {
  535. Multiline = false;
  536. // BUGBUG: Setting properties should not have side-effects like this. Multiline and AllowsTab should be independent.
  537. AllowsTab = false;
  538. }
  539. SetNeedsDraw ();
  540. }
  541. }
  542. /// <summary>
  543. /// Gets or sets whether the <see cref="TextView"/> inserts a tab character into the text or ignores tab input. If
  544. /// set to `false` and the user presses the tab key (or shift-tab) the focus will move to the next view (or previous
  545. /// with shift-tab). The default is `true`; if the user presses the tab key, a tab character will be inserted into the
  546. /// text.
  547. /// </summary>
  548. public bool AllowsTab
  549. {
  550. get => _allowsTab;
  551. set
  552. {
  553. _allowsTab = value;
  554. if (_allowsTab && _tabWidth == 0)
  555. {
  556. _tabWidth = 4;
  557. }
  558. if (_allowsTab && !_multiline)
  559. {
  560. Multiline = true;
  561. }
  562. if (!_allowsTab && _tabWidth > 0)
  563. {
  564. _tabWidth = 0;
  565. }
  566. SetNeedsDraw ();
  567. }
  568. }
  569. /// <summary>
  570. /// Provides autocomplete context menu based on suggestions at the current cursor position. Configure
  571. /// <see cref="IAutocomplete.SuggestionGenerator"/> to enable this feature
  572. /// </summary>
  573. public IAutocomplete Autocomplete { get; protected set; } = new TextViewAutocomplete ();
  574. /// <summary>Get the Context Menu.</summary>
  575. public PopoverMenu? ContextMenu { get; private set; }
  576. /// <summary>Gets the cursor column.</summary>
  577. /// <value>The cursor column.</value>
  578. public int CurrentColumn { get; private set; }
  579. /// <summary>Gets the current cursor row.</summary>
  580. public int CurrentRow { get; private set; }
  581. /// <summary>Sets or gets the current cursor position.</summary>
  582. public Point CursorPosition
  583. {
  584. get => new (CurrentColumn, CurrentRow);
  585. set
  586. {
  587. List<Cell> line = _model.GetLine (Math.Max (Math.Min (value.Y, _model.Count - 1), 0));
  588. CurrentColumn = value.X < 0 ? 0 :
  589. value.X > line.Count ? line.Count : value.X;
  590. CurrentRow = value.Y < 0 ? 0 :
  591. value.Y > _model.Count - 1 ? Math.Max (_model.Count - 1, 0) : value.Y;
  592. SetNeedsDraw ();
  593. Adjust ();
  594. }
  595. }
  596. /// <summary>
  597. /// Indicates whatever the text has history changes or not. <see langword="true"/> if the text has history changes
  598. /// <see langword="false"/> otherwise.
  599. /// </summary>
  600. public bool HasHistoryChanges => _historyText.HasHistoryChanges;
  601. /// <summary>
  602. /// If <see langword="true"/> and the current <see cref="Cell.Attribute"/> is null will inherit from the
  603. /// previous, otherwise if <see langword="false"/> (default) do nothing. If the text is load with
  604. /// <see cref="Load(List{Cell})"/> this property is automatically sets to <see langword="true"/>.
  605. /// </summary>
  606. public bool InheritsPreviousAttribute { get; set; }
  607. /// <summary>
  608. /// Indicates whatever the text was changed or not. <see langword="true"/> if the text was changed
  609. /// <see langword="false"/> otherwise.
  610. /// </summary>
  611. public bool IsDirty
  612. {
  613. get => _historyText.IsDirty (_model.GetAllLines ());
  614. set => _historyText.Clear (_model.GetAllLines ());
  615. }
  616. /// <summary>Gets or sets the left column.</summary>
  617. public int LeftColumn
  618. {
  619. get => _leftColumn;
  620. set
  621. {
  622. if (value > 0 && _wordWrap)
  623. {
  624. return;
  625. }
  626. _leftColumn = Math.Max (Math.Min (value, Maxlength - 1), 0);
  627. }
  628. }
  629. /// <summary>Gets the number of lines.</summary>
  630. public int Lines => _model.Count;
  631. /// <summary>Gets the maximum visible length line.</summary>
  632. public int Maxlength => _model.GetMaxVisibleLine (_topRow, _topRow + Viewport.Height, TabWidth);
  633. /// <summary>Gets or sets a value indicating whether this <see cref="TextView"/> is a multiline text view.</summary>
  634. public bool Multiline
  635. {
  636. get => _multiline;
  637. set
  638. {
  639. _multiline = value;
  640. if (_multiline && !_allowsTab)
  641. {
  642. AllowsTab = true;
  643. }
  644. if (_multiline && !_allowsReturn)
  645. {
  646. AllowsReturn = true;
  647. }
  648. if (!_multiline)
  649. {
  650. AllowsReturn = false;
  651. AllowsTab = false;
  652. WordWrap = false;
  653. CurrentColumn = 0;
  654. CurrentRow = 0;
  655. _savedHeight = Height;
  656. Height = Dim.Auto (DimAutoStyle.Text, 1);
  657. if (!IsInitialized)
  658. {
  659. _model.LoadString (Text);
  660. }
  661. SetNeedsDraw ();
  662. }
  663. else if (_multiline && _savedHeight is { })
  664. {
  665. Height = _savedHeight;
  666. SetNeedsDraw ();
  667. }
  668. KeyBindings.Remove (Key.Enter);
  669. KeyBindings.Add (Key.Enter, Multiline ? Command.NewLine : Command.Accept);
  670. }
  671. }
  672. /// <summary>Gets or sets whether the <see cref="TextView"/> is in read-only mode or not</summary>
  673. /// <value>Boolean value(Default false)</value>
  674. public bool ReadOnly
  675. {
  676. get => _isReadOnly;
  677. set
  678. {
  679. if (value != _isReadOnly)
  680. {
  681. _isReadOnly = value;
  682. SetNeedsDraw ();
  683. WrapTextModel ();
  684. Adjust ();
  685. }
  686. }
  687. }
  688. /// <summary>Length of the selected text.</summary>
  689. public int SelectedLength => GetSelectedLength ();
  690. /// <summary>
  691. /// Gets the selected text as
  692. /// <see>
  693. /// <cref>List{List{Cell}}</cref>
  694. /// </see>
  695. /// </summary>
  696. public List<List<Cell>> SelectedCellsList
  697. {
  698. get
  699. {
  700. GetRegion (out List<List<Cell>> selectedCellsList);
  701. return selectedCellsList;
  702. }
  703. }
  704. /// <summary>The selected text.</summary>
  705. public string SelectedText
  706. {
  707. get
  708. {
  709. if (!IsSelecting || (_model.Count == 1 && _model.GetLine (0).Count == 0))
  710. {
  711. return string.Empty;
  712. }
  713. return GetSelectedRegion ();
  714. }
  715. }
  716. /// <summary>Get or sets whether the user is currently selecting text.</summary>
  717. public bool IsSelecting { get; set; }
  718. /// <summary>Start column position of the selected text.</summary>
  719. public int SelectionStartColumn
  720. {
  721. get => _selectionStartColumn;
  722. set
  723. {
  724. List<Cell> line = _model.GetLine (_selectionStartRow);
  725. _selectionStartColumn = value < 0 ? 0 :
  726. value > line.Count ? line.Count : value;
  727. IsSelecting = true;
  728. SetNeedsDraw ();
  729. Adjust ();
  730. }
  731. }
  732. /// <summary>Start row position of the selected text.</summary>
  733. public int SelectionStartRow
  734. {
  735. get => _selectionStartRow;
  736. set
  737. {
  738. _selectionStartRow = value < 0 ? 0 :
  739. value > _model.Count - 1 ? Math.Max (_model.Count - 1, 0) : value;
  740. IsSelecting = true;
  741. SetNeedsDraw ();
  742. Adjust ();
  743. }
  744. }
  745. /// <summary>Gets or sets a value indicating the number of whitespace when pressing the TAB key.</summary>
  746. public int TabWidth
  747. {
  748. get => _tabWidth;
  749. set
  750. {
  751. _tabWidth = Math.Max (value, 0);
  752. if (_tabWidth > 0 && !AllowsTab)
  753. {
  754. AllowsTab = true;
  755. }
  756. SetNeedsDraw ();
  757. }
  758. }
  759. /// <summary>Sets or gets the text in the <see cref="TextView"/>.</summary>
  760. /// <remarks>
  761. /// The <see cref="View.TextChanged"/> event is fired whenever this property is set. Note, however, that Text is not
  762. /// set by <see cref="TextView"/> as the user types.
  763. /// </remarks>
  764. public override string Text
  765. {
  766. get
  767. {
  768. if (_wordWrap)
  769. {
  770. return _wrapManager!.Model.ToString ();
  771. }
  772. return _model.ToString ();
  773. }
  774. set
  775. {
  776. ResetPosition ();
  777. _model.LoadString (value);
  778. if (_wordWrap)
  779. {
  780. _wrapManager = new (_model);
  781. _model = _wrapManager.WrapModel (Viewport.Width, out _, out _, out _, out _);
  782. }
  783. OnTextChanged ();
  784. SetNeedsDraw ();
  785. _historyText.Clear (_model.GetAllLines ());
  786. }
  787. }
  788. /// <summary>Gets or sets the top row.</summary>
  789. public int TopRow
  790. {
  791. get => _topRow;
  792. set => _topRow = Math.Max (Math.Min (value, Lines - 1), 0);
  793. }
  794. /// <summary>
  795. /// Tracks whether the text view should be considered "used", that is, that the user has moved in the entry, so
  796. /// new input should be appended at the cursor position, rather than clearing the entry
  797. /// </summary>
  798. public bool Used { get; set; }
  799. /// <summary>Allows word wrap the to fit the available container width.</summary>
  800. public bool WordWrap
  801. {
  802. get => _wordWrap;
  803. set
  804. {
  805. if (value == _wordWrap)
  806. {
  807. return;
  808. }
  809. if (value && !_multiline)
  810. {
  811. return;
  812. }
  813. _wordWrap = value;
  814. ResetPosition ();
  815. if (_wordWrap)
  816. {
  817. _wrapManager = new (_model);
  818. WrapTextModel ();
  819. }
  820. else if (!_wordWrap && _wrapManager is { })
  821. {
  822. _model = _wrapManager.Model;
  823. }
  824. SetNeedsDraw ();
  825. }
  826. }
  827. /// <summary>
  828. /// Gets or sets whether the word forward and word backward navigation should use the same or equivalent rune type.
  829. /// Default is <c>false</c> meaning using equivalent rune type.
  830. /// </summary>
  831. public bool UseSameRuneTypeForWords { get; set; }
  832. /// <summary>
  833. /// Gets or sets whether the word navigation should select only the word itself without spaces around it or with the
  834. /// spaces at right.
  835. /// Default is <c>false</c> meaning that the spaces at right are included in the selection.
  836. /// </summary>
  837. public bool SelectWordOnlyOnDoubleClick { get; set; }
  838. /// <summary>Allows clearing the <see cref="HistoryTextItemEventArgs"/> items updating the original text.</summary>
  839. public void ClearHistoryChanges () { _historyText?.Clear (_model.GetAllLines ()); }
  840. /// <summary>Closes the contents of the stream into the <see cref="TextView"/>.</summary>
  841. /// <returns><c>true</c>, if stream was closed, <c>false</c> otherwise.</returns>
  842. public bool CloseFile ()
  843. {
  844. SetWrapModel ();
  845. bool res = _model.CloseFile ();
  846. ResetPosition ();
  847. SetNeedsDraw ();
  848. UpdateWrapModel ();
  849. return res;
  850. }
  851. /// <summary>Raised when the contents of the <see cref="TextView"/> are changed.</summary>
  852. /// <remarks>
  853. /// Unlike the <see cref="View.TextChanged"/> event, this event is raised whenever the user types or otherwise changes
  854. /// the contents of the <see cref="TextView"/>.
  855. /// </remarks>
  856. public event EventHandler<ContentsChangedEventArgs>? ContentsChanged;
  857. internal void ApplyCellsAttribute (Attribute attribute)
  858. {
  859. if (!ReadOnly && SelectedLength > 0)
  860. {
  861. int startRow = Math.Min (SelectionStartRow, CurrentRow);
  862. int endRow = Math.Max (CurrentRow, SelectionStartRow);
  863. int startCol = SelectionStartRow <= CurrentRow ? SelectionStartColumn : CurrentColumn;
  864. int endCol = CurrentRow >= SelectionStartRow ? CurrentColumn : SelectionStartColumn;
  865. List<List<Cell>> selectedCellsOriginal = [];
  866. List<List<Cell>> selectedCellsChanged = [];
  867. for (int r = startRow; r <= endRow; r++)
  868. {
  869. List<Cell> line = GetLine (r);
  870. selectedCellsOriginal.Add ([.. line]);
  871. for (int c = r == startRow ? startCol : 0;
  872. c < (r == endRow ? endCol : line.Count);
  873. c++)
  874. {
  875. Cell cell = line [c]; // Copy value to a new variable
  876. cell.Attribute = attribute; // Modify the copy
  877. line [c] = cell; // Assign the modified copy back
  878. }
  879. selectedCellsChanged.Add ([.. GetLine (r)]);
  880. }
  881. GetSelectedRegion ();
  882. IsSelecting = false;
  883. _historyText.Add (
  884. [.. selectedCellsOriginal],
  885. new Point (startCol, startRow)
  886. );
  887. _historyText.Add (
  888. [.. selectedCellsChanged],
  889. new Point (startCol, startRow),
  890. TextEditingLineStatus.Attribute
  891. );
  892. }
  893. }
  894. private Attribute? GetSelectedCellAttribute ()
  895. {
  896. List<Cell> line;
  897. if (SelectedLength > 0)
  898. {
  899. line = GetLine (SelectionStartRow);
  900. if (line [Math.Min (SelectionStartColumn, line.Count - 1)].Attribute is { } attributeSel)
  901. {
  902. return new (attributeSel);
  903. }
  904. return GetAttributeForRole (VisualRole.Active);
  905. }
  906. line = GetCurrentLine ();
  907. if (line [Math.Min (CurrentColumn, line.Count - 1)].Attribute is { } attribute)
  908. {
  909. return new (attribute);
  910. }
  911. return GetAttributeForRole (VisualRole.Active);
  912. }
  913. /// <summary>
  914. /// Open a dialog to set the foreground and background colors.
  915. /// </summary>
  916. public void PromptForColors ()
  917. {
  918. if (!ColorPicker.Prompt (
  919. "Colors",
  920. GetSelectedCellAttribute (),
  921. out Attribute newAttribute
  922. ))
  923. {
  924. return;
  925. }
  926. var attribute = new Attribute (
  927. newAttribute.Foreground,
  928. newAttribute.Background,
  929. newAttribute.Style
  930. );
  931. ApplyCellsAttribute (attribute);
  932. }
  933. private string? _copiedText;
  934. private List<List<Cell>> _copiedCellsList = [];
  935. /// <summary>Copy the selected text to the clipboard contents.</summary>
  936. public void Copy ()
  937. {
  938. SetWrapModel ();
  939. if (IsSelecting)
  940. {
  941. _copiedText = GetRegion (out _copiedCellsList);
  942. SetClipboard (_copiedText);
  943. _copyWithoutSelection = false;
  944. }
  945. else
  946. {
  947. List<Cell> currentLine = GetCurrentLine ();
  948. _copiedCellsList.Add (currentLine);
  949. _copiedText = Cell.ToString (currentLine);
  950. SetClipboard (_copiedText);
  951. _copyWithoutSelection = true;
  952. }
  953. UpdateWrapModel ();
  954. DoNeededAction ();
  955. }
  956. /// <summary>Cut the selected text to the clipboard contents.</summary>
  957. public void Cut ()
  958. {
  959. SetWrapModel ();
  960. _copiedText = GetRegion (out _copiedCellsList);
  961. SetClipboard (_copiedText);
  962. if (!_isReadOnly)
  963. {
  964. ClearRegion ();
  965. _historyText.Add (
  966. [new (GetCurrentLine ())],
  967. CursorPosition,
  968. TextEditingLineStatus.Replaced
  969. );
  970. }
  971. UpdateWrapModel ();
  972. IsSelecting = false;
  973. DoNeededAction ();
  974. OnContentsChanged ();
  975. }
  976. /// <summary>Deletes all text.</summary>
  977. public void DeleteAll ()
  978. {
  979. if (Lines == 0)
  980. {
  981. return;
  982. }
  983. _selectionStartColumn = 0;
  984. _selectionStartRow = 0;
  985. MoveBottomEndExtend ();
  986. DeleteCharLeft ();
  987. SetNeedsDraw ();
  988. }
  989. /// <summary>Deletes all the selected or a single character at left from the position of the cursor.</summary>
  990. public void DeleteCharLeft ()
  991. {
  992. if (_isReadOnly)
  993. {
  994. return;
  995. }
  996. SetWrapModel ();
  997. if (IsSelecting)
  998. {
  999. _historyText.Add (new () { new (GetCurrentLine ()) }, CursorPosition);
  1000. ClearSelectedRegion ();
  1001. List<Cell> currentLine = GetCurrentLine ();
  1002. _historyText.Add (
  1003. new () { new (currentLine) },
  1004. CursorPosition,
  1005. TextEditingLineStatus.Replaced
  1006. );
  1007. UpdateWrapModel ();
  1008. OnContentsChanged ();
  1009. return;
  1010. }
  1011. if (DeleteTextBackwards ())
  1012. {
  1013. UpdateWrapModel ();
  1014. OnContentsChanged ();
  1015. return;
  1016. }
  1017. UpdateWrapModel ();
  1018. DoNeededAction ();
  1019. OnContentsChanged ();
  1020. }
  1021. /// <summary>Deletes all the selected or a single character at right from the position of the cursor.</summary>
  1022. public void DeleteCharRight ()
  1023. {
  1024. if (_isReadOnly)
  1025. {
  1026. return;
  1027. }
  1028. SetWrapModel ();
  1029. if (IsSelecting)
  1030. {
  1031. _historyText.Add (new () { new (GetCurrentLine ()) }, CursorPosition);
  1032. ClearSelectedRegion ();
  1033. List<Cell> currentLine = GetCurrentLine ();
  1034. _historyText.Add (
  1035. new () { new (currentLine) },
  1036. CursorPosition,
  1037. TextEditingLineStatus.Replaced
  1038. );
  1039. UpdateWrapModel ();
  1040. OnContentsChanged ();
  1041. return;
  1042. }
  1043. if (DeleteTextForwards ())
  1044. {
  1045. UpdateWrapModel ();
  1046. OnContentsChanged ();
  1047. return;
  1048. }
  1049. UpdateWrapModel ();
  1050. DoNeededAction ();
  1051. OnContentsChanged ();
  1052. }
  1053. /// <summary>Invoked when the normal color is drawn.</summary>
  1054. public event EventHandler<CellEventArgs>? DrawNormalColor;
  1055. /// <summary>Invoked when the ready only color is drawn.</summary>
  1056. public event EventHandler<CellEventArgs>? DrawReadOnlyColor;
  1057. /// <summary>Invoked when the selection color is drawn.</summary>
  1058. public event EventHandler<CellEventArgs>? DrawSelectionColor;
  1059. /// <summary>
  1060. /// Invoked when the used color is drawn. The Used Color is used to indicate if the <see cref="Key.InsertChar"/>
  1061. /// was pressed and enabled.
  1062. /// </summary>
  1063. public event EventHandler<CellEventArgs>? DrawUsedColor;
  1064. /// <summary>Find the next text based on the match case with the option to replace it.</summary>
  1065. /// <param name="textToFind">The text to find.</param>
  1066. /// <param name="gaveFullTurn"><c>true</c>If all the text was forward searched.<c>false</c>otherwise.</param>
  1067. /// <param name="matchCase">The match case setting.</param>
  1068. /// <param name="matchWholeWord">The match whole word setting.</param>
  1069. /// <param name="textToReplace">The text to replace.</param>
  1070. /// <param name="replace"><c>true</c>If is replacing.<c>false</c>otherwise.</param>
  1071. /// <returns><c>true</c>If the text was found.<c>false</c>otherwise.</returns>
  1072. public bool FindNextText (
  1073. string textToFind,
  1074. out bool gaveFullTurn,
  1075. bool matchCase = false,
  1076. bool matchWholeWord = false,
  1077. string? textToReplace = null,
  1078. bool replace = false
  1079. )
  1080. {
  1081. if (_model.Count == 0)
  1082. {
  1083. gaveFullTurn = false;
  1084. return false;
  1085. }
  1086. SetWrapModel ();
  1087. ResetContinuousFind ();
  1088. (Point current, bool found) foundPos =
  1089. _model.FindNextText (textToFind, out gaveFullTurn, matchCase, matchWholeWord);
  1090. return SetFoundText (textToFind, foundPos, textToReplace, replace);
  1091. }
  1092. /// <summary>Find the previous text based on the match case with the option to replace it.</summary>
  1093. /// <param name="textToFind">The text to find.</param>
  1094. /// <param name="gaveFullTurn"><c>true</c>If all the text was backward searched.<c>false</c>otherwise.</param>
  1095. /// <param name="matchCase">The match case setting.</param>
  1096. /// <param name="matchWholeWord">The match whole word setting.</param>
  1097. /// <param name="textToReplace">The text to replace.</param>
  1098. /// <param name="replace"><c>true</c>If the text was found.<c>false</c>otherwise.</param>
  1099. /// <returns><c>true</c>If the text was found.<c>false</c>otherwise.</returns>
  1100. public bool FindPreviousText (
  1101. string textToFind,
  1102. out bool gaveFullTurn,
  1103. bool matchCase = false,
  1104. bool matchWholeWord = false,
  1105. string? textToReplace = null,
  1106. bool replace = false
  1107. )
  1108. {
  1109. if (_model.Count == 0)
  1110. {
  1111. gaveFullTurn = false;
  1112. return false;
  1113. }
  1114. SetWrapModel ();
  1115. ResetContinuousFind ();
  1116. (Point current, bool found) foundPos =
  1117. _model.FindPreviousText (textToFind, out gaveFullTurn, matchCase, matchWholeWord);
  1118. return SetFoundText (textToFind, foundPos, textToReplace, replace);
  1119. }
  1120. /// <summary>Reset the flag to stop continuous find.</summary>
  1121. public void FindTextChanged () { _continuousFind = false; }
  1122. /// <summary>Gets all lines of characters.</summary>
  1123. /// <returns></returns>
  1124. public List<List<Cell>> GetAllLines () { return _model.GetAllLines (); }
  1125. /// <summary>
  1126. /// Returns the characters on the current line (where the cursor is positioned). Use <see cref="CurrentColumn"/>
  1127. /// to determine the position of the cursor within that line
  1128. /// </summary>
  1129. /// <returns></returns>
  1130. public List<Cell> GetCurrentLine () { return _model.GetLine (CurrentRow); }
  1131. /// <summary>Returns the characters on the <paramref name="line"/>.</summary>
  1132. /// <param name="line">The intended line.</param>
  1133. /// <returns></returns>
  1134. public List<Cell> GetLine (int line) { return _model.GetLine (line); }
  1135. /// <inheritdoc/>
  1136. protected override bool OnGettingAttributeForRole (in VisualRole role, ref Attribute currentAttribute)
  1137. {
  1138. if (role == VisualRole.Normal)
  1139. {
  1140. currentAttribute = GetAttributeForRole (VisualRole.Editable);
  1141. return true;
  1142. }
  1143. return base.OnGettingAttributeForRole (role, ref currentAttribute);
  1144. }
  1145. /// <summary>
  1146. /// Inserts the given <paramref name="toAdd"/> text at the current cursor position exactly as if the user had just
  1147. /// typed it
  1148. /// </summary>
  1149. /// <param name="toAdd">Text to add</param>
  1150. public void InsertText (string toAdd)
  1151. {
  1152. foreach (char ch in toAdd)
  1153. {
  1154. Key key;
  1155. try
  1156. {
  1157. key = new (ch);
  1158. }
  1159. catch (Exception)
  1160. {
  1161. throw new ArgumentException (
  1162. $"Cannot insert character '{ch}' because it does not map to a Key"
  1163. );
  1164. }
  1165. InsertText (key);
  1166. if (NeedsDraw)
  1167. {
  1168. Adjust ();
  1169. }
  1170. else
  1171. {
  1172. PositionCursor ();
  1173. }
  1174. }
  1175. }
  1176. /// <summary>Loads the contents of the file into the <see cref="TextView"/>.</summary>
  1177. /// <returns><c>true</c>, if file was loaded, <c>false</c> otherwise.</returns>
  1178. /// <param name="path">Path to the file to load.</param>
  1179. public bool Load (string path)
  1180. {
  1181. SetWrapModel ();
  1182. bool res;
  1183. try
  1184. {
  1185. SetWrapModel ();
  1186. res = _model.LoadFile (path);
  1187. _historyText.Clear (_model.GetAllLines ());
  1188. ResetPosition ();
  1189. }
  1190. finally
  1191. {
  1192. UpdateWrapModel ();
  1193. SetNeedsDraw ();
  1194. Adjust ();
  1195. }
  1196. UpdateWrapModel ();
  1197. return res;
  1198. }
  1199. /// <summary>Loads the contents of the stream into the <see cref="TextView"/>.</summary>
  1200. /// <returns><c>true</c>, if stream was loaded, <c>false</c> otherwise.</returns>
  1201. /// <param name="stream">Stream to load the contents from.</param>
  1202. public void Load (Stream stream)
  1203. {
  1204. SetWrapModel ();
  1205. _model.LoadStream (stream);
  1206. _historyText.Clear (_model.GetAllLines ());
  1207. ResetPosition ();
  1208. SetNeedsDraw ();
  1209. UpdateWrapModel ();
  1210. }
  1211. /// <summary>Loads the contents of the <see cref="Cell"/> list into the <see cref="TextView"/>.</summary>
  1212. /// <param name="cells">Rune cells list to load the contents from.</param>
  1213. public void Load (List<Cell> cells)
  1214. {
  1215. SetWrapModel ();
  1216. _model.LoadCells (cells, GetAttributeForRole (VisualRole.Focus));
  1217. _historyText.Clear (_model.GetAllLines ());
  1218. ResetPosition ();
  1219. SetNeedsDraw ();
  1220. UpdateWrapModel ();
  1221. InheritsPreviousAttribute = true;
  1222. }
  1223. /// <summary>Loads the contents of the list of <see cref="Cell"/> list into the <see cref="TextView"/>.</summary>
  1224. /// <param name="cellsList">List of rune cells list to load the contents from.</param>
  1225. public void Load (List<List<Cell>> cellsList)
  1226. {
  1227. SetWrapModel ();
  1228. InheritsPreviousAttribute = true;
  1229. _model.LoadListCells (cellsList, GetAttributeForRole (VisualRole.Focus));
  1230. _historyText.Clear (_model.GetAllLines ());
  1231. ResetPosition ();
  1232. SetNeedsDraw ();
  1233. UpdateWrapModel ();
  1234. }
  1235. /// <inheritdoc/>
  1236. protected override bool OnMouseEvent (MouseEventArgs ev)
  1237. {
  1238. if (ev is { IsSingleDoubleOrTripleClicked: false, IsPressed: false, IsReleased: false, IsWheel: false }
  1239. && !ev.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition)
  1240. && !ev.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ButtonShift)
  1241. && !ev.Flags.HasFlag (MouseFlags.Button1DoubleClicked | MouseFlags.ButtonShift)
  1242. && !ev.Flags.HasFlag (ContextMenu!.MouseFlags))
  1243. {
  1244. return false;
  1245. }
  1246. if (!CanFocus)
  1247. {
  1248. return true;
  1249. }
  1250. if (!HasFocus)
  1251. {
  1252. SetFocus ();
  1253. }
  1254. _continuousFind = false;
  1255. // Give autocomplete first opportunity to respond to mouse clicks
  1256. if (SelectedLength == 0 && Autocomplete.OnMouseEvent (ev, true))
  1257. {
  1258. return true;
  1259. }
  1260. if (ev.Flags == MouseFlags.Button1Clicked)
  1261. {
  1262. if (_isButtonReleased)
  1263. {
  1264. _isButtonReleased = false;
  1265. if (SelectedLength == 0)
  1266. {
  1267. StopSelecting ();
  1268. }
  1269. return true;
  1270. }
  1271. if (_shiftSelecting && !_isButtonShift)
  1272. {
  1273. StopSelecting ();
  1274. }
  1275. ProcessMouseClick (ev, out _);
  1276. if (Used)
  1277. {
  1278. PositionCursor ();
  1279. }
  1280. else
  1281. {
  1282. SetNeedsDraw ();
  1283. }
  1284. _lastWasKill = false;
  1285. _columnTrack = CurrentColumn;
  1286. }
  1287. else if (ev.Flags == MouseFlags.WheeledDown)
  1288. {
  1289. _lastWasKill = false;
  1290. _columnTrack = CurrentColumn;
  1291. ScrollTo (_topRow + 1);
  1292. }
  1293. else if (ev.Flags == MouseFlags.WheeledUp)
  1294. {
  1295. _lastWasKill = false;
  1296. _columnTrack = CurrentColumn;
  1297. ScrollTo (_topRow - 1);
  1298. }
  1299. else if (ev.Flags == MouseFlags.WheeledRight)
  1300. {
  1301. _lastWasKill = false;
  1302. _columnTrack = CurrentColumn;
  1303. ScrollTo (_leftColumn + 1, false);
  1304. }
  1305. else if (ev.Flags == MouseFlags.WheeledLeft)
  1306. {
  1307. _lastWasKill = false;
  1308. _columnTrack = CurrentColumn;
  1309. ScrollTo (_leftColumn - 1, false);
  1310. }
  1311. else if (ev.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  1312. {
  1313. ProcessMouseClick (ev, out List<Cell> line);
  1314. PositionCursor ();
  1315. if (_model.Count > 0 && _shiftSelecting && IsSelecting)
  1316. {
  1317. if (CurrentRow - _topRow >= Viewport.Height - 1 && _model.Count > _topRow + CurrentRow)
  1318. {
  1319. ScrollTo (_topRow + Viewport.Height);
  1320. }
  1321. else if (_topRow > 0 && CurrentRow <= _topRow)
  1322. {
  1323. ScrollTo (_topRow - Viewport.Height);
  1324. }
  1325. else if (ev.Position.Y >= Viewport.Height)
  1326. {
  1327. ScrollTo (_model.Count);
  1328. }
  1329. else if (ev.Position.Y < 0 && _topRow > 0)
  1330. {
  1331. ScrollTo (0);
  1332. }
  1333. if (CurrentColumn - _leftColumn >= Viewport.Width - 1 && line.Count > _leftColumn + CurrentColumn)
  1334. {
  1335. ScrollTo (_leftColumn + Viewport.Width, false);
  1336. }
  1337. else if (_leftColumn > 0 && CurrentColumn <= _leftColumn)
  1338. {
  1339. ScrollTo (_leftColumn - Viewport.Width, false);
  1340. }
  1341. else if (ev.Position.X >= Viewport.Width)
  1342. {
  1343. ScrollTo (line.Count, false);
  1344. }
  1345. else if (ev.Position.X < 0 && _leftColumn > 0)
  1346. {
  1347. ScrollTo (0, false);
  1348. }
  1349. }
  1350. _lastWasKill = false;
  1351. _columnTrack = CurrentColumn;
  1352. }
  1353. else if (ev.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ButtonShift))
  1354. {
  1355. if (!_shiftSelecting)
  1356. {
  1357. _isButtonShift = true;
  1358. StartSelecting ();
  1359. }
  1360. ProcessMouseClick (ev, out _);
  1361. PositionCursor ();
  1362. _lastWasKill = false;
  1363. _columnTrack = CurrentColumn;
  1364. }
  1365. else if (ev.Flags.HasFlag (MouseFlags.Button1Pressed))
  1366. {
  1367. if (_shiftSelecting)
  1368. {
  1369. _clickWithSelecting = true;
  1370. StopSelecting ();
  1371. }
  1372. ProcessMouseClick (ev, out _);
  1373. PositionCursor ();
  1374. if (!IsSelecting)
  1375. {
  1376. StartSelecting ();
  1377. }
  1378. _lastWasKill = false;
  1379. _columnTrack = CurrentColumn;
  1380. if (Application.Mouse.MouseGrabView is null)
  1381. {
  1382. Application.Mouse.GrabMouse (this);
  1383. }
  1384. }
  1385. else if (ev.Flags.HasFlag (MouseFlags.Button1Released))
  1386. {
  1387. _isButtonReleased = true;
  1388. Application.Mouse.UngrabMouse ();
  1389. }
  1390. else if (ev.Flags.HasFlag (MouseFlags.Button1DoubleClicked))
  1391. {
  1392. if (ev.Flags.HasFlag (MouseFlags.ButtonShift))
  1393. {
  1394. if (!IsSelecting)
  1395. {
  1396. StartSelecting ();
  1397. }
  1398. }
  1399. else if (IsSelecting)
  1400. {
  1401. StopSelecting ();
  1402. }
  1403. ProcessMouseClick (ev, out List<Cell> line);
  1404. if (!IsSelecting)
  1405. {
  1406. StartSelecting ();
  1407. }
  1408. (int startCol, int col, int row)? newPos = _model.ProcessDoubleClickSelection (SelectionStartColumn, CurrentColumn, CurrentRow, UseSameRuneTypeForWords, SelectWordOnlyOnDoubleClick);
  1409. if (newPos.HasValue)
  1410. {
  1411. SelectionStartColumn = newPos.Value.startCol;
  1412. CurrentColumn = newPos.Value.col;
  1413. CurrentRow = newPos.Value.row;
  1414. }
  1415. PositionCursor ();
  1416. _lastWasKill = false;
  1417. _columnTrack = CurrentColumn;
  1418. SetNeedsDraw ();
  1419. }
  1420. else if (ev.Flags.HasFlag (MouseFlags.Button1TripleClicked))
  1421. {
  1422. if (IsSelecting)
  1423. {
  1424. StopSelecting ();
  1425. }
  1426. ProcessMouseClick (ev, out List<Cell> line);
  1427. CurrentColumn = 0;
  1428. if (!IsSelecting)
  1429. {
  1430. StartSelecting ();
  1431. }
  1432. CurrentColumn = line.Count;
  1433. PositionCursor ();
  1434. _lastWasKill = false;
  1435. _columnTrack = CurrentColumn;
  1436. SetNeedsDraw ();
  1437. }
  1438. else if (ev.Flags == ContextMenu!.MouseFlags)
  1439. {
  1440. ShowContextMenu (ev.ScreenPosition);
  1441. }
  1442. OnUnwrappedCursorPosition ();
  1443. return true;
  1444. }
  1445. /// <summary>Will scroll the <see cref="TextView"/> to the last line and position the cursor there.</summary>
  1446. public void MoveEnd ()
  1447. {
  1448. CurrentRow = _model.Count - 1;
  1449. List<Cell> line = GetCurrentLine ();
  1450. CurrentColumn = line.Count;
  1451. TrackColumn ();
  1452. DoNeededAction ();
  1453. }
  1454. /// <summary>Will scroll the <see cref="TextView"/> to the first line and position the cursor there.</summary>
  1455. public void MoveHome ()
  1456. {
  1457. CurrentRow = 0;
  1458. _topRow = 0;
  1459. CurrentColumn = 0;
  1460. _leftColumn = 0;
  1461. TrackColumn ();
  1462. DoNeededAction ();
  1463. }
  1464. /// <summary>
  1465. /// Called when the contents of the TextView change. E.g. when the user types text or deletes text. Raises the
  1466. /// <see cref="ContentsChanged"/> event.
  1467. /// </summary>
  1468. public virtual void OnContentsChanged ()
  1469. {
  1470. ContentsChanged?.Invoke (this, new (CurrentRow, CurrentColumn));
  1471. ProcessInheritsPreviousScheme (CurrentRow, CurrentColumn);
  1472. ProcessAutocomplete ();
  1473. }
  1474. /// <inheritdoc/>
  1475. protected override bool OnDrawingContent ()
  1476. {
  1477. _isDrawing = true;
  1478. SetAttributeForRole (Enabled ? VisualRole.Editable : VisualRole.Disabled);
  1479. (int width, int height) offB = OffSetBackground ();
  1480. int right = Viewport.Width + offB.width;
  1481. int bottom = Viewport.Height + offB.height;
  1482. var row = 0;
  1483. for (int idxRow = _topRow; idxRow < _model.Count; idxRow++)
  1484. {
  1485. List<Cell> line = _model.GetLine (idxRow);
  1486. int lineRuneCount = line.Count;
  1487. var col = 0;
  1488. Move (0, row);
  1489. for (int idxCol = _leftColumn; idxCol < lineRuneCount; idxCol++)
  1490. {
  1491. Rune rune = idxCol >= lineRuneCount ? (Rune)' ' : line [idxCol].Rune;
  1492. int cols = rune.GetColumns ();
  1493. if (idxCol < line.Count && IsSelecting && PointInSelection (idxCol, idxRow))
  1494. {
  1495. OnDrawSelectionColor (line, idxCol, idxRow);
  1496. }
  1497. else if (idxCol == CurrentColumn && idxRow == CurrentRow && !IsSelecting && !Used && HasFocus && idxCol < lineRuneCount)
  1498. {
  1499. OnDrawUsedColor (line, idxCol, idxRow);
  1500. }
  1501. else if (ReadOnly)
  1502. {
  1503. OnDrawReadOnlyColor (line, idxCol, idxRow);
  1504. }
  1505. else
  1506. {
  1507. OnDrawNormalColor (line, idxCol, idxRow);
  1508. }
  1509. if (rune.Value == '\t')
  1510. {
  1511. cols += TabWidth + 1;
  1512. if (col + cols > right)
  1513. {
  1514. cols = right - col;
  1515. }
  1516. for (var i = 0; i < cols; i++)
  1517. {
  1518. if (col + i < right)
  1519. {
  1520. AddRune (col + i, row, (Rune)' ');
  1521. }
  1522. }
  1523. }
  1524. else
  1525. {
  1526. AddRune (col, row, rune);
  1527. // Ensures that cols less than 0 to be 1 because it will be converted to a printable rune
  1528. cols = Math.Max (cols, 1);
  1529. }
  1530. if (!TextModel.SetCol (ref col, Viewport.Right, cols))
  1531. {
  1532. break;
  1533. }
  1534. if (idxCol + 1 < lineRuneCount && col + line [idxCol + 1].Rune.GetColumns () > right)
  1535. {
  1536. break;
  1537. }
  1538. }
  1539. if (col < right)
  1540. {
  1541. SetAttributeForRole (ReadOnly ? VisualRole.ReadOnly : VisualRole.Editable);
  1542. ClearRegion (col, row, right, row + 1);
  1543. }
  1544. row++;
  1545. }
  1546. if (row < bottom)
  1547. {
  1548. SetAttributeForRole (ReadOnly ? VisualRole.ReadOnly : VisualRole.Editable);
  1549. ClearRegion (Viewport.Left, row, right, bottom);
  1550. }
  1551. _isDrawing = false;
  1552. return false;
  1553. }
  1554. /// <inheritdoc/>
  1555. protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? view)
  1556. {
  1557. if (Application.Mouse.MouseGrabView is { } && Application.Mouse.MouseGrabView == this)
  1558. {
  1559. Application.Mouse.UngrabMouse ();
  1560. }
  1561. }
  1562. /// <inheritdoc/>
  1563. protected override bool OnKeyDown (Key key)
  1564. {
  1565. if (!key.IsValid)
  1566. {
  1567. return false;
  1568. }
  1569. // Give autocomplete first opportunity to respond to key presses
  1570. if (SelectedLength == 0 && Autocomplete.Suggestions.Count > 0 && Autocomplete.ProcessKey (key))
  1571. {
  1572. return true;
  1573. }
  1574. return false;
  1575. }
  1576. /// <inheritdoc/>
  1577. protected override bool OnKeyDownNotHandled (Key a)
  1578. {
  1579. if (!CanFocus)
  1580. {
  1581. return true;
  1582. }
  1583. ResetColumnTrack ();
  1584. // Ignore control characters and other special keys
  1585. if (!a.IsKeyCodeAtoZ && (a.KeyCode < KeyCode.Space || a.KeyCode > KeyCode.CharMask))
  1586. {
  1587. return false;
  1588. }
  1589. InsertText (a);
  1590. DoNeededAction ();
  1591. return true;
  1592. }
  1593. /// <inheritdoc/>
  1594. public override bool OnKeyUp (Key key)
  1595. {
  1596. if (key == Key.Space.WithCtrl)
  1597. {
  1598. return true;
  1599. }
  1600. return false;
  1601. }
  1602. /// <summary>Invoke the <see cref="UnwrappedCursorPosition"/> event with the unwrapped <see cref="CursorPosition"/>.</summary>
  1603. public virtual void OnUnwrappedCursorPosition (int? cRow = null, int? cCol = null)
  1604. {
  1605. int? row = cRow ?? CurrentRow;
  1606. int? col = cCol ?? CurrentColumn;
  1607. if (cRow is null && cCol is null && _wordWrap)
  1608. {
  1609. row = _wrapManager!.GetModelLineFromWrappedLines (CurrentRow);
  1610. col = _wrapManager.GetModelColFromWrappedLines (CurrentRow, CurrentColumn);
  1611. }
  1612. UnwrappedCursorPosition?.Invoke (this, new (col.Value, row.Value));
  1613. }
  1614. /// <summary>Paste the clipboard contents into the current selected position.</summary>
  1615. public void Paste ()
  1616. {
  1617. if (_isReadOnly)
  1618. {
  1619. return;
  1620. }
  1621. SetWrapModel ();
  1622. string? contents = Clipboard.Contents;
  1623. if (_copyWithoutSelection && contents!.FirstOrDefault (x => x is '\n' or '\r') == 0)
  1624. {
  1625. List<Cell> runeList = contents is null ? [] : Cell.ToCellList (contents);
  1626. List<Cell> currentLine = GetCurrentLine ();
  1627. _historyText.Add ([new (currentLine)], CursorPosition);
  1628. List<List<Cell>> addedLine = [new (currentLine), runeList];
  1629. _historyText.Add (
  1630. [.. addedLine],
  1631. CursorPosition,
  1632. TextEditingLineStatus.Added
  1633. );
  1634. _model.AddLine (CurrentRow, runeList);
  1635. CurrentRow++;
  1636. _historyText.Add (
  1637. [new (GetCurrentLine ())],
  1638. CursorPosition,
  1639. TextEditingLineStatus.Replaced
  1640. );
  1641. SetNeedsDraw ();
  1642. OnContentsChanged ();
  1643. }
  1644. else
  1645. {
  1646. if (IsSelecting)
  1647. {
  1648. ClearRegion ();
  1649. }
  1650. _copyWithoutSelection = false;
  1651. InsertAllText (contents!, true);
  1652. if (IsSelecting)
  1653. {
  1654. _historyText.ReplaceLast (
  1655. [new (GetCurrentLine ())],
  1656. CursorPosition,
  1657. TextEditingLineStatus.Original
  1658. );
  1659. }
  1660. SetNeedsDraw ();
  1661. }
  1662. UpdateWrapModel ();
  1663. IsSelecting = false;
  1664. DoNeededAction ();
  1665. }
  1666. /// <summary>Positions the cursor on the current row and column</summary>
  1667. public override Point? PositionCursor ()
  1668. {
  1669. ProcessAutocomplete ();
  1670. if (!CanFocus || !Enabled || Application.Driver is null)
  1671. {
  1672. return null;
  1673. }
  1674. if (Application.Mouse.MouseGrabView == this && IsSelecting)
  1675. {
  1676. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  1677. //var minRow = Math.Min (Math.Max (Math.Min (selectionStartRow, currentRow) - topRow, 0), Viewport.Height);
  1678. //var maxRow = Math.Min (Math.Max (Math.Max (selectionStartRow, currentRow) - topRow, 0), Viewport.Height);
  1679. //SetNeedsDraw (new (0, minRow, Viewport.Width, maxRow));
  1680. SetNeedsDraw ();
  1681. }
  1682. List<Cell> line = _model.GetLine (CurrentRow);
  1683. var col = 0;
  1684. if (line.Count > 0)
  1685. {
  1686. for (int idx = _leftColumn; idx < line.Count; idx++)
  1687. {
  1688. if (idx >= CurrentColumn)
  1689. {
  1690. break;
  1691. }
  1692. int cols = line [idx].Rune.GetColumns ();
  1693. if (line [idx].Rune.Value == '\t')
  1694. {
  1695. cols += TabWidth + 1;
  1696. }
  1697. else
  1698. {
  1699. // Ensures that cols less than 0 to be 1 because it will be converted to a printable rune
  1700. cols = Math.Max (cols, 1);
  1701. }
  1702. if (!TextModel.SetCol (ref col, Viewport.Width, cols))
  1703. {
  1704. col = CurrentColumn;
  1705. break;
  1706. }
  1707. }
  1708. }
  1709. int posX = CurrentColumn - _leftColumn;
  1710. int posY = CurrentRow - _topRow;
  1711. if (posX > -1 && col >= posX && posX < Viewport.Width && _topRow <= CurrentRow && posY < Viewport.Height)
  1712. {
  1713. Move (col, CurrentRow - _topRow);
  1714. return new (col, CurrentRow - _topRow);
  1715. }
  1716. return null; // Hide cursor
  1717. }
  1718. /// <summary>Redoes the latest changes.</summary>
  1719. public void Redo ()
  1720. {
  1721. if (ReadOnly)
  1722. {
  1723. return;
  1724. }
  1725. _historyText.Redo ();
  1726. }
  1727. /// <summary>Replaces all the text based on the match case.</summary>
  1728. /// <param name="textToFind">The text to find.</param>
  1729. /// <param name="matchCase">The match case setting.</param>
  1730. /// <param name="matchWholeWord">The match whole word setting.</param>
  1731. /// <param name="textToReplace">The text to replace.</param>
  1732. /// <returns><c>true</c>If the text was found.<c>false</c>otherwise.</returns>
  1733. public bool ReplaceAllText (
  1734. string textToFind,
  1735. bool matchCase = false,
  1736. bool matchWholeWord = false,
  1737. string? textToReplace = null
  1738. )
  1739. {
  1740. if (_isReadOnly || _model.Count == 0)
  1741. {
  1742. return false;
  1743. }
  1744. SetWrapModel ();
  1745. ResetContinuousFind ();
  1746. (Point current, bool found) foundPos =
  1747. _model.ReplaceAllText (textToFind, matchCase, matchWholeWord, textToReplace);
  1748. return SetFoundText (textToFind, foundPos, textToReplace, false, true);
  1749. }
  1750. /// <summary>
  1751. /// Will scroll the <see cref="TextView"/> to display the specified row at the top if <paramref name="isRow"/> is
  1752. /// true or will scroll the <see cref="TextView"/> to display the specified column at the left if
  1753. /// <paramref name="isRow"/> is false.
  1754. /// </summary>
  1755. /// <param name="idx">
  1756. /// Row that should be displayed at the top or Column that should be displayed at the left, if the value
  1757. /// is negative it will be reset to zero
  1758. /// </param>
  1759. /// <param name="isRow">If true (default) the <paramref name="idx"/> is a row, column otherwise.</param>
  1760. public void ScrollTo (int idx, bool isRow = true)
  1761. {
  1762. if (idx < 0)
  1763. {
  1764. idx = 0;
  1765. }
  1766. if (isRow)
  1767. {
  1768. _topRow = Math.Max (idx > _model.Count - 1 ? _model.Count - 1 : idx, 0);
  1769. }
  1770. else if (!_wordWrap)
  1771. {
  1772. int maxlength =
  1773. _model.GetMaxVisibleLine (_topRow, _topRow + Viewport.Height, TabWidth);
  1774. _leftColumn = Math.Max (!_wordWrap && idx > maxlength - 1 ? maxlength - 1 : idx, 0);
  1775. }
  1776. SetNeedsDraw ();
  1777. }
  1778. /// <summary>Select all text.</summary>
  1779. public void SelectAll ()
  1780. {
  1781. if (_model.Count == 0)
  1782. {
  1783. return;
  1784. }
  1785. StartSelecting ();
  1786. _selectionStartColumn = 0;
  1787. _selectionStartRow = 0;
  1788. CurrentColumn = _model.GetLine (_model.Count - 1).Count;
  1789. CurrentRow = _model.Count - 1;
  1790. SetNeedsDraw ();
  1791. }
  1792. ///// <summary>Raised when the <see cref="Text"/> property of the <see cref="TextView"/> changes.</summary>
  1793. ///// <remarks>
  1794. ///// The <see cref="Text"/> property of <see cref="TextView"/> only changes when it is explicitly set, not as the
  1795. ///// user types. To be notified as the user changes the contents of the TextView see <see cref="IsDirty"/>.
  1796. ///// </remarks>
  1797. //public event EventHandler? TextChanged;
  1798. /// <summary>Undoes the latest changes.</summary>
  1799. public void Undo ()
  1800. {
  1801. if (ReadOnly)
  1802. {
  1803. return;
  1804. }
  1805. _historyText.Undo ();
  1806. }
  1807. /// <summary>Invoked with the unwrapped <see cref="CursorPosition"/>.</summary>
  1808. public event EventHandler<Point>? UnwrappedCursorPosition;
  1809. /// <summary>
  1810. /// Sets the <see cref="View.Driver"/> to an appropriate color for rendering the given <paramref name="idxCol"/>
  1811. /// of the current <paramref name="line"/>. Override to provide custom coloring by calling
  1812. /// <see cref="View.SetAttribute"/> Defaults to <see cref="Scheme.Normal"/>.
  1813. /// </summary>
  1814. /// <param name="line">The line.</param>
  1815. /// <param name="idxCol">The col index.</param>
  1816. /// <param name="idxRow">The row index.</param>
  1817. protected virtual void OnDrawNormalColor (List<Cell> line, int idxCol, int idxRow)
  1818. {
  1819. (int Row, int Col) unwrappedPos = GetUnwrappedPosition (idxRow, idxCol);
  1820. var ev = new CellEventArgs (line, idxCol, unwrappedPos);
  1821. DrawNormalColor?.Invoke (this, ev);
  1822. if (line [idxCol].Attribute is { })
  1823. {
  1824. Attribute? attribute = line [idxCol].Attribute;
  1825. SetAttribute ((Attribute)attribute!);
  1826. }
  1827. else
  1828. {
  1829. SetAttribute (GetAttributeForRole (VisualRole.Normal));
  1830. }
  1831. }
  1832. /// <summary>
  1833. /// Sets the <see cref="View.Driver"/> to an appropriate color for rendering the given <paramref name="idxCol"/>
  1834. /// of the current <paramref name="line"/>. Override to provide custom coloring by calling
  1835. /// <see cref="View.SetAttribute(Attribute)"/> Defaults to <see cref="Scheme.Focus"/>.
  1836. /// </summary>
  1837. /// <param name="line">The line.</param>
  1838. /// <param name="idxCol">The col index.</param>
  1839. /// ///
  1840. /// <param name="idxRow">The row index.</param>
  1841. protected virtual void OnDrawReadOnlyColor (List<Cell> line, int idxCol, int idxRow)
  1842. {
  1843. (int Row, int Col) unwrappedPos = GetUnwrappedPosition (idxRow, idxCol);
  1844. var ev = new CellEventArgs (line, idxCol, unwrappedPos);
  1845. DrawReadOnlyColor?.Invoke (this, ev);
  1846. Attribute? cellAttribute = line [idxCol].Attribute is { } ? line [idxCol].Attribute : GetAttributeForRole (VisualRole.ReadOnly);
  1847. if (cellAttribute!.Value.Foreground == cellAttribute.Value.Background)
  1848. {
  1849. SetAttribute (new (cellAttribute.Value.Foreground, cellAttribute.Value.Background, cellAttribute.Value.Style));
  1850. }
  1851. else
  1852. {
  1853. SetAttributeForRole (VisualRole.ReadOnly);
  1854. }
  1855. }
  1856. /// <summary>
  1857. /// Sets the <see cref="View.Driver"/> to an appropriate color for rendering the given <paramref name="idxCol"/>
  1858. /// of the current <paramref name="line"/>. Override to provide custom coloring by calling
  1859. /// <see cref="View.SetAttribute(Attribute)"/> Defaults to <see cref="Scheme.Focus"/>.
  1860. /// </summary>
  1861. /// <param name="line">The line.</param>
  1862. /// <param name="idxCol">The col index.</param>
  1863. /// ///
  1864. /// <param name="idxRow">The row index.</param>
  1865. protected virtual void OnDrawSelectionColor (List<Cell> line, int idxCol, int idxRow)
  1866. {
  1867. (int Row, int Col) unwrappedPos = GetUnwrappedPosition (idxRow, idxCol);
  1868. var ev = new CellEventArgs (line, idxCol, unwrappedPos);
  1869. DrawSelectionColor?.Invoke (this, ev);
  1870. if (line [idxCol].Attribute is { })
  1871. {
  1872. Attribute? attribute = line [idxCol].Attribute;
  1873. Attribute? active = GetAttributeForRole (VisualRole.Active);
  1874. SetAttribute (new (active!.Value.Foreground, active.Value.Background, attribute!.Value.Style));
  1875. }
  1876. else
  1877. {
  1878. SetAttributeForRole (VisualRole.Active);
  1879. }
  1880. }
  1881. /// <summary>
  1882. /// Sets the <see cref="View.Driver"/> to an appropriate color for rendering the given <paramref name="idxCol"/>
  1883. /// of the current <paramref name="line"/>. Override to provide custom coloring by calling
  1884. /// <see cref="View.SetAttribute(Attribute)"/> Defaults to <see cref="Scheme.HotFocus"/>.
  1885. /// </summary>
  1886. /// <param name="line">The line.</param>
  1887. /// <param name="idxCol">The col index.</param>
  1888. /// ///
  1889. /// <param name="idxRow">The row index.</param>
  1890. protected virtual void OnDrawUsedColor (List<Cell> line, int idxCol, int idxRow)
  1891. {
  1892. (int Row, int Col) unwrappedPos = GetUnwrappedPosition (idxRow, idxCol);
  1893. var ev = new CellEventArgs (line, idxCol, unwrappedPos);
  1894. DrawUsedColor?.Invoke (this, ev);
  1895. if (line [idxCol].Attribute is { })
  1896. {
  1897. Attribute? attribute = line [idxCol].Attribute;
  1898. SetValidUsedColor (attribute!);
  1899. }
  1900. else
  1901. {
  1902. SetValidUsedColor (GetAttributeForRole (VisualRole.Focus));
  1903. }
  1904. }
  1905. private void Adjust ()
  1906. {
  1907. (int width, int height) offB = OffSetBackground ();
  1908. List<Cell> line = GetCurrentLine ();
  1909. bool need = NeedsDraw || _wrapNeeded || !Used;
  1910. (int size, int length) tSize = TextModel.DisplaySize (line, -1, -1, false, TabWidth);
  1911. (int size, int length) dSize = TextModel.DisplaySize (line, _leftColumn, CurrentColumn, true, TabWidth);
  1912. if (!_wordWrap && CurrentColumn < _leftColumn)
  1913. {
  1914. _leftColumn = CurrentColumn;
  1915. need = true;
  1916. }
  1917. else if (!_wordWrap
  1918. && (CurrentColumn - _leftColumn + 1 > Viewport.Width + offB.width || dSize.size + 1 >= Viewport.Width + offB.width))
  1919. {
  1920. _leftColumn = TextModel.CalculateLeftColumn (
  1921. line,
  1922. _leftColumn,
  1923. CurrentColumn,
  1924. Viewport.Width + offB.width,
  1925. TabWidth
  1926. );
  1927. need = true;
  1928. }
  1929. else if ((_wordWrap && _leftColumn > 0) || (dSize.size < Viewport.Width + offB.width && tSize.size < Viewport.Width + offB.width))
  1930. {
  1931. if (_leftColumn > 0)
  1932. {
  1933. _leftColumn = 0;
  1934. need = true;
  1935. }
  1936. }
  1937. if (CurrentRow < _topRow)
  1938. {
  1939. _topRow = CurrentRow;
  1940. need = true;
  1941. }
  1942. else if (CurrentRow - _topRow >= Viewport.Height + offB.height)
  1943. {
  1944. _topRow = Math.Min (Math.Max (CurrentRow - Viewport.Height + 1, 0), CurrentRow);
  1945. need = true;
  1946. }
  1947. else if (_topRow > 0 && CurrentRow < _topRow)
  1948. {
  1949. _topRow = Math.Max (_topRow - 1, 0);
  1950. need = true;
  1951. }
  1952. if (need)
  1953. {
  1954. if (_wrapNeeded)
  1955. {
  1956. WrapTextModel ();
  1957. _wrapNeeded = false;
  1958. }
  1959. SetNeedsDraw ();
  1960. }
  1961. else
  1962. {
  1963. if (IsInitialized)
  1964. {
  1965. PositionCursor ();
  1966. }
  1967. }
  1968. OnUnwrappedCursorPosition ();
  1969. }
  1970. private void AppendClipboard (string text) { Clipboard.Contents += text; }
  1971. private PopoverMenu CreateContextMenu ()
  1972. {
  1973. PopoverMenu menu = new (
  1974. new List<View>
  1975. {
  1976. new MenuItemv2 (this, Command.SelectAll, Strings.ctxSelectAll),
  1977. new MenuItemv2 (this, Command.DeleteAll, Strings.ctxDeleteAll),
  1978. new MenuItemv2 (this, Command.Copy, Strings.ctxCopy),
  1979. new MenuItemv2 (this, Command.Cut, Strings.ctxCut),
  1980. new MenuItemv2 (this, Command.Paste, Strings.ctxPaste),
  1981. new MenuItemv2 (this, Command.Undo, Strings.ctxUndo),
  1982. new MenuItemv2 (this, Command.Redo, Strings.ctxRedo)
  1983. });
  1984. menu.KeyChanged += ContextMenu_KeyChanged;
  1985. return menu;
  1986. }
  1987. private void ClearRegion (int left, int top, int right, int bottom)
  1988. {
  1989. for (int row = top; row < bottom; row++)
  1990. {
  1991. Move (left, row);
  1992. for (int col = left; col < right; col++)
  1993. {
  1994. AddRune (col, row, (Rune)' ');
  1995. }
  1996. }
  1997. }
  1998. //
  1999. // Clears the contents of the selected region
  2000. //
  2001. private void ClearRegion ()
  2002. {
  2003. SetWrapModel ();
  2004. long start, end;
  2005. long currentEncoded = ((long)(uint)CurrentRow << 32) | (uint)CurrentColumn;
  2006. GetEncodedRegionBounds (out start, out end);
  2007. var startRow = (int)(start >> 32);
  2008. var maxrow = (int)(end >> 32);
  2009. var startCol = (int)(start & 0xffffffff);
  2010. var endCol = (int)(end & 0xffffffff);
  2011. List<Cell> line = _model.GetLine (startRow);
  2012. _historyText.Add (new () { new (line) }, new (startCol, startRow));
  2013. List<List<Cell>> removedLines = new ();
  2014. if (startRow == maxrow)
  2015. {
  2016. removedLines.Add (new (line));
  2017. line.RemoveRange (startCol, endCol - startCol);
  2018. CurrentColumn = startCol;
  2019. if (_wordWrap)
  2020. {
  2021. SetNeedsDraw ();
  2022. }
  2023. else
  2024. {
  2025. //QUESTION: Is the below comment still relevant?
  2026. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  2027. //SetNeedsDraw (new (0, startRow - topRow, Viewport.Width, startRow - topRow + 1));
  2028. SetNeedsDraw ();
  2029. }
  2030. _historyText.Add (
  2031. new (removedLines),
  2032. CursorPosition,
  2033. TextEditingLineStatus.Removed
  2034. );
  2035. UpdateWrapModel ();
  2036. return;
  2037. }
  2038. removedLines.Add (new (line));
  2039. line.RemoveRange (startCol, line.Count - startCol);
  2040. List<Cell> line2 = _model.GetLine (maxrow);
  2041. line.AddRange (line2.Skip (endCol));
  2042. for (int row = startRow + 1; row <= maxrow; row++)
  2043. {
  2044. removedLines.Add (new (_model.GetLine (startRow + 1)));
  2045. _model.RemoveLine (startRow + 1);
  2046. }
  2047. if (currentEncoded == end)
  2048. {
  2049. CurrentRow -= maxrow - startRow;
  2050. }
  2051. CurrentColumn = startCol;
  2052. _historyText.Add (
  2053. new (removedLines),
  2054. CursorPosition,
  2055. TextEditingLineStatus.Removed
  2056. );
  2057. UpdateWrapModel ();
  2058. SetNeedsDraw ();
  2059. }
  2060. private void ClearSelectedRegion ()
  2061. {
  2062. SetWrapModel ();
  2063. if (!_isReadOnly)
  2064. {
  2065. ClearRegion ();
  2066. }
  2067. UpdateWrapModel ();
  2068. IsSelecting = false;
  2069. DoNeededAction ();
  2070. }
  2071. private void ContextMenu_KeyChanged (object? sender, KeyChangedEventArgs e) { KeyBindings.Replace (e.OldKey, e.NewKey); }
  2072. private bool DeleteTextBackwards ()
  2073. {
  2074. SetWrapModel ();
  2075. if (CurrentColumn > 0)
  2076. {
  2077. // Delete backwards
  2078. List<Cell> currentLine = GetCurrentLine ();
  2079. _historyText.Add (new () { new (currentLine) }, CursorPosition);
  2080. currentLine.RemoveAt (CurrentColumn - 1);
  2081. if (_wordWrap)
  2082. {
  2083. _wrapNeeded = true;
  2084. }
  2085. CurrentColumn--;
  2086. _historyText.Add (
  2087. new () { new (currentLine) },
  2088. CursorPosition,
  2089. TextEditingLineStatus.Replaced
  2090. );
  2091. if (CurrentColumn < _leftColumn)
  2092. {
  2093. _leftColumn--;
  2094. SetNeedsDraw ();
  2095. }
  2096. else
  2097. {
  2098. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  2099. //SetNeedsDraw (new (0, currentRow - topRow, 1, Viewport.Width));
  2100. SetNeedsDraw ();
  2101. }
  2102. }
  2103. else
  2104. {
  2105. // Merges the current line with the previous one.
  2106. if (CurrentRow == 0)
  2107. {
  2108. return true;
  2109. }
  2110. int prowIdx = CurrentRow - 1;
  2111. List<Cell> prevRow = _model.GetLine (prowIdx);
  2112. _historyText.Add (new () { new (prevRow) }, CursorPosition);
  2113. List<List<Cell>> removedLines = new () { new (prevRow) };
  2114. removedLines.Add (new (GetCurrentLine ()));
  2115. _historyText.Add (
  2116. removedLines,
  2117. new (CurrentColumn, prowIdx),
  2118. TextEditingLineStatus.Removed
  2119. );
  2120. int prevCount = prevRow.Count;
  2121. _model.GetLine (prowIdx).AddRange (GetCurrentLine ());
  2122. _model.RemoveLine (CurrentRow);
  2123. if (_wordWrap)
  2124. {
  2125. _wrapNeeded = true;
  2126. }
  2127. CurrentRow--;
  2128. _historyText.Add (
  2129. new () { GetCurrentLine () },
  2130. new (CurrentColumn, prowIdx),
  2131. TextEditingLineStatus.Replaced
  2132. );
  2133. CurrentColumn = prevCount;
  2134. SetNeedsDraw ();
  2135. }
  2136. UpdateWrapModel ();
  2137. return false;
  2138. }
  2139. private bool DeleteTextForwards ()
  2140. {
  2141. SetWrapModel ();
  2142. List<Cell> currentLine = GetCurrentLine ();
  2143. if (CurrentColumn == currentLine.Count)
  2144. {
  2145. if (CurrentRow + 1 == _model.Count)
  2146. {
  2147. UpdateWrapModel ();
  2148. return true;
  2149. }
  2150. _historyText.Add (new () { new (currentLine) }, CursorPosition);
  2151. List<List<Cell>> removedLines = new () { new (currentLine) };
  2152. List<Cell> nextLine = _model.GetLine (CurrentRow + 1);
  2153. removedLines.Add (new (nextLine));
  2154. _historyText.Add (removedLines, CursorPosition, TextEditingLineStatus.Removed);
  2155. currentLine.AddRange (nextLine);
  2156. _model.RemoveLine (CurrentRow + 1);
  2157. _historyText.Add (
  2158. new () { new (currentLine) },
  2159. CursorPosition,
  2160. TextEditingLineStatus.Replaced
  2161. );
  2162. if (_wordWrap)
  2163. {
  2164. _wrapNeeded = true;
  2165. }
  2166. DoSetNeedsDraw (new (0, CurrentRow - _topRow, Viewport.Width, CurrentRow - _topRow + 1));
  2167. }
  2168. else
  2169. {
  2170. _historyText.Add ([ [.. currentLine]], CursorPosition);
  2171. currentLine.RemoveAt (CurrentColumn);
  2172. _historyText.Add (
  2173. [ [.. currentLine]],
  2174. CursorPosition,
  2175. TextEditingLineStatus.Replaced
  2176. );
  2177. if (_wordWrap)
  2178. {
  2179. _wrapNeeded = true;
  2180. }
  2181. DoSetNeedsDraw (
  2182. new (
  2183. CurrentColumn - _leftColumn,
  2184. CurrentRow - _topRow,
  2185. Viewport.Width,
  2186. Math.Max (CurrentRow - _topRow + 1, 0)
  2187. )
  2188. );
  2189. }
  2190. UpdateWrapModel ();
  2191. return false;
  2192. }
  2193. private void DoNeededAction ()
  2194. {
  2195. if (!NeedsDraw && (IsSelecting || _wrapNeeded || !Used))
  2196. {
  2197. SetNeedsDraw ();
  2198. }
  2199. if (NeedsDraw)
  2200. {
  2201. Adjust ();
  2202. }
  2203. else
  2204. {
  2205. PositionCursor ();
  2206. OnUnwrappedCursorPosition ();
  2207. }
  2208. }
  2209. private void DoSetNeedsDraw (Rectangle rect)
  2210. {
  2211. if (_wrapNeeded)
  2212. {
  2213. SetNeedsDraw ();
  2214. }
  2215. else
  2216. {
  2217. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  2218. //SetNeedsDraw (rect);
  2219. SetNeedsDraw ();
  2220. }
  2221. }
  2222. private IEnumerable<(int col, int row, Cell rune)> ForwardIterator (int col, int row)
  2223. {
  2224. if (col < 0 || row < 0)
  2225. {
  2226. yield break;
  2227. }
  2228. if (row >= _model.Count)
  2229. {
  2230. yield break;
  2231. }
  2232. List<Cell> line = GetCurrentLine ();
  2233. if (col >= line.Count)
  2234. {
  2235. yield break;
  2236. }
  2237. while (row < _model.Count)
  2238. {
  2239. for (int c = col; c < line.Count; c++)
  2240. {
  2241. yield return (c, row, line [c]);
  2242. }
  2243. col = 0;
  2244. row++;
  2245. line = GetCurrentLine ();
  2246. }
  2247. }
  2248. private void GenerateSuggestions ()
  2249. {
  2250. List<Cell> currentLine = GetCurrentLine ();
  2251. int cursorPosition = Math.Min (CurrentColumn, currentLine.Count);
  2252. Autocomplete.Context = new (
  2253. currentLine,
  2254. cursorPosition,
  2255. Autocomplete.Context != null
  2256. ? Autocomplete.Context.Canceled
  2257. : false
  2258. );
  2259. Autocomplete.GenerateSuggestions (
  2260. Autocomplete.Context
  2261. );
  2262. }
  2263. // Returns an encoded region start..end (top 32 bits are the row, low32 the column)
  2264. private void GetEncodedRegionBounds (
  2265. out long start,
  2266. out long end,
  2267. int? startRow = null,
  2268. int? startCol = null,
  2269. int? cRow = null,
  2270. int? cCol = null
  2271. )
  2272. {
  2273. long selection;
  2274. long point;
  2275. if (startRow is null || startCol is null || cRow is null || cCol is null)
  2276. {
  2277. selection = ((long)(uint)_selectionStartRow << 32) | (uint)_selectionStartColumn;
  2278. point = ((long)(uint)CurrentRow << 32) | (uint)CurrentColumn;
  2279. }
  2280. else
  2281. {
  2282. selection = ((long)(uint)startRow << 32) | (uint)startCol;
  2283. point = ((long)(uint)cRow << 32) | (uint)cCol;
  2284. }
  2285. if (selection > point)
  2286. {
  2287. start = point;
  2288. end = selection;
  2289. }
  2290. else
  2291. {
  2292. start = selection;
  2293. end = point;
  2294. }
  2295. }
  2296. //
  2297. // Returns a string with the text in the selected
  2298. // region.
  2299. //
  2300. internal string GetRegion (
  2301. out List<List<Cell>> cellsList,
  2302. int? sRow = null,
  2303. int? sCol = null,
  2304. int? cRow = null,
  2305. int? cCol = null,
  2306. TextModel? model = null
  2307. )
  2308. {
  2309. GetEncodedRegionBounds (out long start, out long end, sRow, sCol, cRow, cCol);
  2310. cellsList = [];
  2311. if (start == end)
  2312. {
  2313. return string.Empty;
  2314. }
  2315. var startRow = (int)(start >> 32);
  2316. var maxRow = (int)(end >> 32);
  2317. var startCol = (int)(start & 0xffffffff);
  2318. var endCol = (int)(end & 0xffffffff);
  2319. List<Cell> line = model is null ? _model.GetLine (startRow) : model.GetLine (startRow);
  2320. List<Cell> cells;
  2321. if (startRow == maxRow)
  2322. {
  2323. cells = line.GetRange (startCol, endCol - startCol);
  2324. cellsList.Add (cells);
  2325. return StringFromRunes (cells);
  2326. }
  2327. cells = line.GetRange (startCol, line.Count - startCol);
  2328. cellsList.Add (cells);
  2329. string res = StringFromRunes (cells);
  2330. for (int row = startRow + 1; row < maxRow; row++)
  2331. {
  2332. cellsList.AddRange ([]);
  2333. cells = model == null ? _model.GetLine (row) : model.GetLine (row);
  2334. cellsList.Add (cells);
  2335. res = res
  2336. + Environment.NewLine
  2337. + StringFromRunes (cells);
  2338. }
  2339. line = model is null ? _model.GetLine (maxRow) : model.GetLine (maxRow);
  2340. cellsList.AddRange ([]);
  2341. cells = line.GetRange (0, endCol);
  2342. cellsList.Add (cells);
  2343. res = res + Environment.NewLine + StringFromRunes (cells);
  2344. return res;
  2345. }
  2346. private int GetSelectedLength () { return SelectedText.Length; }
  2347. private string GetSelectedRegion ()
  2348. {
  2349. int cRow = CurrentRow;
  2350. int cCol = CurrentColumn;
  2351. int startRow = _selectionStartRow;
  2352. int startCol = _selectionStartColumn;
  2353. TextModel model = _model;
  2354. if (_wordWrap)
  2355. {
  2356. cRow = _wrapManager!.GetModelLineFromWrappedLines (CurrentRow);
  2357. cCol = _wrapManager.GetModelColFromWrappedLines (CurrentRow, CurrentColumn);
  2358. startRow = _wrapManager.GetModelLineFromWrappedLines (_selectionStartRow);
  2359. startCol = _wrapManager.GetModelColFromWrappedLines (_selectionStartRow, _selectionStartColumn);
  2360. model = _wrapManager.Model;
  2361. }
  2362. OnUnwrappedCursorPosition (cRow, cCol);
  2363. return GetRegion (out _, startRow, startCol, cRow, cCol, model);
  2364. }
  2365. private (int Row, int Col) GetUnwrappedPosition (int line, int col)
  2366. {
  2367. if (WordWrap)
  2368. {
  2369. return new ValueTuple<int, int> (
  2370. _wrapManager!.GetModelLineFromWrappedLines (line),
  2371. _wrapManager.GetModelColFromWrappedLines (line, col)
  2372. );
  2373. }
  2374. return new ValueTuple<int, int> (line, col);
  2375. }
  2376. private void HistoryText_ChangeText (object sender, HistoryTextItemEventArgs obj)
  2377. {
  2378. SetWrapModel ();
  2379. if (obj is { })
  2380. {
  2381. int startLine = obj.CursorPosition.Y;
  2382. if (obj.RemovedOnAdded is { })
  2383. {
  2384. int offset;
  2385. if (obj.IsUndoing)
  2386. {
  2387. offset = Math.Max (obj.RemovedOnAdded.Lines.Count - obj.Lines.Count, 1);
  2388. }
  2389. else
  2390. {
  2391. offset = obj.RemovedOnAdded.Lines.Count - 1;
  2392. }
  2393. for (var i = 0; i < offset; i++)
  2394. {
  2395. if (Lines > obj.RemovedOnAdded.CursorPosition.Y)
  2396. {
  2397. _model.RemoveLine (obj.RemovedOnAdded.CursorPosition.Y);
  2398. }
  2399. else
  2400. {
  2401. break;
  2402. }
  2403. }
  2404. }
  2405. for (var i = 0; i < obj.Lines.Count; i++)
  2406. {
  2407. if (i == 0 || obj.LineStatus == TextEditingLineStatus.Original || obj.LineStatus == TextEditingLineStatus.Attribute)
  2408. {
  2409. _model.ReplaceLine (startLine, obj.Lines [i]);
  2410. }
  2411. else if (obj is { IsUndoing: true, LineStatus: TextEditingLineStatus.Removed }
  2412. or { IsUndoing: false, LineStatus: TextEditingLineStatus.Added })
  2413. {
  2414. _model.AddLine (startLine, obj.Lines [i]);
  2415. }
  2416. else if (Lines > obj.CursorPosition.Y + 1)
  2417. {
  2418. _model.RemoveLine (obj.CursorPosition.Y + 1);
  2419. }
  2420. startLine++;
  2421. }
  2422. CursorPosition = obj.FinalCursorPosition;
  2423. }
  2424. UpdateWrapModel ();
  2425. Adjust ();
  2426. OnContentsChanged ();
  2427. }
  2428. private void Insert (Cell cell)
  2429. {
  2430. List<Cell> line = GetCurrentLine ();
  2431. if (Used)
  2432. {
  2433. line.Insert (Math.Min (CurrentColumn, line.Count), cell);
  2434. }
  2435. else
  2436. {
  2437. if (CurrentColumn < line.Count)
  2438. {
  2439. line.RemoveAt (CurrentColumn);
  2440. }
  2441. line.Insert (Math.Min (CurrentColumn, line.Count), cell);
  2442. }
  2443. int prow = CurrentRow - _topRow;
  2444. if (!_wrapNeeded)
  2445. {
  2446. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  2447. //SetNeedsDraw (new (0, prow, Math.Max (Viewport.Width, 0), Math.Max (prow + 1, 0)));
  2448. SetNeedsDraw ();
  2449. }
  2450. }
  2451. private void InsertAllText (string text, bool fromClipboard = false)
  2452. {
  2453. if (string.IsNullOrEmpty (text))
  2454. {
  2455. return;
  2456. }
  2457. List<List<Cell>> lines;
  2458. if (fromClipboard && text == _copiedText)
  2459. {
  2460. lines = _copiedCellsList;
  2461. }
  2462. else
  2463. {
  2464. // Get selected attribute
  2465. Attribute? attribute = GetSelectedAttribute (CurrentRow, CurrentColumn);
  2466. lines = Cell.StringToLinesOfCells (text, attribute);
  2467. }
  2468. if (lines.Count == 0)
  2469. {
  2470. return;
  2471. }
  2472. SetWrapModel ();
  2473. List<Cell> line = GetCurrentLine ();
  2474. _historyText.Add ([new (line)], CursorPosition);
  2475. // Optimize single line
  2476. if (lines.Count == 1)
  2477. {
  2478. line.InsertRange (CurrentColumn, lines [0]);
  2479. CurrentColumn += lines [0].Count;
  2480. _historyText.Add (
  2481. [new (line)],
  2482. CursorPosition,
  2483. TextEditingLineStatus.Replaced
  2484. );
  2485. if (!_wordWrap && CurrentColumn - _leftColumn > Viewport.Width)
  2486. {
  2487. _leftColumn = Math.Max (CurrentColumn - Viewport.Width + 1, 0);
  2488. }
  2489. if (_wordWrap)
  2490. {
  2491. SetNeedsDraw ();
  2492. }
  2493. else
  2494. {
  2495. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  2496. //SetNeedsDraw (new (0, currentRow - topRow, Viewport.Width, Math.Max (currentRow - topRow + 1, 0)));
  2497. SetNeedsDraw ();
  2498. }
  2499. UpdateWrapModel ();
  2500. OnContentsChanged ();
  2501. return;
  2502. }
  2503. List<Cell>? rest = null;
  2504. var lastPosition = 0;
  2505. if (_model.Count > 0 && line.Count > 0 && !_copyWithoutSelection)
  2506. {
  2507. // Keep a copy of the rest of the line
  2508. int restCount = line.Count - CurrentColumn;
  2509. rest = line.GetRange (CurrentColumn, restCount);
  2510. line.RemoveRange (CurrentColumn, restCount);
  2511. }
  2512. // First line is inserted at the current location, the rest is appended
  2513. line.InsertRange (CurrentColumn, lines [0]);
  2514. //model.AddLine (currentRow, lines [0]);
  2515. List<List<Cell>> addedLines = [new (line)];
  2516. for (var i = 1; i < lines.Count; i++)
  2517. {
  2518. _model.AddLine (CurrentRow + i, lines [i]);
  2519. addedLines.Add ([.. lines [i]]);
  2520. }
  2521. if (rest is { })
  2522. {
  2523. List<Cell> last = _model.GetLine (CurrentRow + lines.Count - 1);
  2524. lastPosition = last.Count;
  2525. last.InsertRange (last.Count, rest);
  2526. addedLines.Last ().InsertRange (addedLines.Last ().Count, rest);
  2527. }
  2528. _historyText.Add (addedLines, CursorPosition, TextEditingLineStatus.Added);
  2529. // Now adjust column and row positions
  2530. CurrentRow += lines.Count - 1;
  2531. CurrentColumn = rest is { } ? lastPosition : lines [^1].Count;
  2532. Adjust ();
  2533. _historyText.Add (
  2534. [new (line)],
  2535. CursorPosition,
  2536. TextEditingLineStatus.Replaced
  2537. );
  2538. UpdateWrapModel ();
  2539. OnContentsChanged ();
  2540. }
  2541. private bool InsertText (Key a, Attribute? attribute = null)
  2542. {
  2543. //So that special keys like tab can be processed
  2544. if (_isReadOnly)
  2545. {
  2546. return true;
  2547. }
  2548. SetWrapModel ();
  2549. _historyText.Add ([new (GetCurrentLine ())], CursorPosition);
  2550. if (IsSelecting)
  2551. {
  2552. ClearSelectedRegion ();
  2553. }
  2554. if ((uint)a.KeyCode == '\n')
  2555. {
  2556. _model.AddLine (CurrentRow + 1, []);
  2557. CurrentRow++;
  2558. CurrentColumn = 0;
  2559. }
  2560. else if ((uint)a.KeyCode == '\r')
  2561. {
  2562. CurrentColumn = 0;
  2563. }
  2564. else
  2565. {
  2566. if (Used)
  2567. {
  2568. Insert (new () { Rune = a.AsRune, Attribute = attribute });
  2569. CurrentColumn++;
  2570. if (CurrentColumn >= _leftColumn + Viewport.Width)
  2571. {
  2572. _leftColumn++;
  2573. SetNeedsDraw ();
  2574. }
  2575. }
  2576. else
  2577. {
  2578. Insert (new () { Rune = a.AsRune, Attribute = attribute });
  2579. CurrentColumn++;
  2580. }
  2581. }
  2582. _historyText.Add (
  2583. [new (GetCurrentLine ())],
  2584. CursorPosition,
  2585. TextEditingLineStatus.Replaced
  2586. );
  2587. UpdateWrapModel ();
  2588. OnContentsChanged ();
  2589. return true;
  2590. }
  2591. private void KillToEndOfLine ()
  2592. {
  2593. if (_isReadOnly)
  2594. {
  2595. return;
  2596. }
  2597. if (_model.Count == 1 && GetCurrentLine ().Count == 0)
  2598. {
  2599. // Prevents from adding line feeds if there is no more lines.
  2600. return;
  2601. }
  2602. SetWrapModel ();
  2603. List<Cell> currentLine = GetCurrentLine ();
  2604. var setLastWasKill = true;
  2605. if (currentLine.Count > 0 && CurrentColumn == currentLine.Count)
  2606. {
  2607. UpdateWrapModel ();
  2608. DeleteTextForwards ();
  2609. return;
  2610. }
  2611. _historyText.Add (new () { new (currentLine) }, CursorPosition);
  2612. if (currentLine.Count == 0)
  2613. {
  2614. if (CurrentRow < _model.Count - 1)
  2615. {
  2616. List<List<Cell>> removedLines = new () { new (currentLine) };
  2617. _model.RemoveLine (CurrentRow);
  2618. removedLines.Add (new (GetCurrentLine ()));
  2619. _historyText.Add (
  2620. new (removedLines),
  2621. CursorPosition,
  2622. TextEditingLineStatus.Removed
  2623. );
  2624. }
  2625. if (_model.Count > 0 || _lastWasKill)
  2626. {
  2627. string val = Environment.NewLine;
  2628. if (_lastWasKill)
  2629. {
  2630. AppendClipboard (val);
  2631. }
  2632. else
  2633. {
  2634. SetClipboard (val);
  2635. }
  2636. }
  2637. if (_model.Count == 0)
  2638. {
  2639. // Prevents from adding line feeds if there is no more lines.
  2640. setLastWasKill = false;
  2641. }
  2642. }
  2643. else
  2644. {
  2645. int restCount = currentLine.Count - CurrentColumn;
  2646. List<Cell> rest = currentLine.GetRange (CurrentColumn, restCount);
  2647. var val = string.Empty;
  2648. val += StringFromRunes (rest);
  2649. if (_lastWasKill)
  2650. {
  2651. AppendClipboard (val);
  2652. }
  2653. else
  2654. {
  2655. SetClipboard (val);
  2656. }
  2657. currentLine.RemoveRange (CurrentColumn, restCount);
  2658. }
  2659. _historyText.Add (
  2660. [ [.. GetCurrentLine ()]],
  2661. CursorPosition,
  2662. TextEditingLineStatus.Replaced
  2663. );
  2664. UpdateWrapModel ();
  2665. DoSetNeedsDraw (new (0, CurrentRow - _topRow, Viewport.Width, Viewport.Height));
  2666. _lastWasKill = setLastWasKill;
  2667. DoNeededAction ();
  2668. }
  2669. private void KillToLeftStart ()
  2670. {
  2671. if (_isReadOnly)
  2672. {
  2673. return;
  2674. }
  2675. if (_model.Count == 1 && GetCurrentLine ().Count == 0)
  2676. {
  2677. // Prevents from adding line feeds if there is no more lines.
  2678. return;
  2679. }
  2680. SetWrapModel ();
  2681. List<Cell> currentLine = GetCurrentLine ();
  2682. var setLastWasKill = true;
  2683. if (currentLine.Count > 0 && CurrentColumn == 0)
  2684. {
  2685. UpdateWrapModel ();
  2686. DeleteTextBackwards ();
  2687. return;
  2688. }
  2689. _historyText.Add ([ [.. currentLine]], CursorPosition);
  2690. if (currentLine.Count == 0)
  2691. {
  2692. if (CurrentRow > 0)
  2693. {
  2694. _model.RemoveLine (CurrentRow);
  2695. if (_model.Count > 0 || _lastWasKill)
  2696. {
  2697. string val = Environment.NewLine;
  2698. if (_lastWasKill)
  2699. {
  2700. AppendClipboard (val);
  2701. }
  2702. else
  2703. {
  2704. SetClipboard (val);
  2705. }
  2706. }
  2707. if (_model.Count == 0)
  2708. {
  2709. // Prevents from adding line feeds if there is no more lines.
  2710. setLastWasKill = false;
  2711. }
  2712. CurrentRow--;
  2713. currentLine = _model.GetLine (CurrentRow);
  2714. List<List<Cell>> removedLine =
  2715. [
  2716. [..currentLine],
  2717. []
  2718. ];
  2719. _historyText.Add (
  2720. [.. removedLine],
  2721. CursorPosition,
  2722. TextEditingLineStatus.Removed
  2723. );
  2724. CurrentColumn = currentLine.Count;
  2725. }
  2726. }
  2727. else
  2728. {
  2729. int restCount = CurrentColumn;
  2730. List<Cell> rest = currentLine.GetRange (0, restCount);
  2731. var val = string.Empty;
  2732. val += StringFromRunes (rest);
  2733. if (_lastWasKill)
  2734. {
  2735. AppendClipboard (val);
  2736. }
  2737. else
  2738. {
  2739. SetClipboard (val);
  2740. }
  2741. currentLine.RemoveRange (0, restCount);
  2742. CurrentColumn = 0;
  2743. }
  2744. _historyText.Add (
  2745. [ [.. GetCurrentLine ()]],
  2746. CursorPosition,
  2747. TextEditingLineStatus.Replaced
  2748. );
  2749. UpdateWrapModel ();
  2750. DoSetNeedsDraw (new (0, CurrentRow - _topRow, Viewport.Width, Viewport.Height));
  2751. _lastWasKill = setLastWasKill;
  2752. DoNeededAction ();
  2753. }
  2754. private void KillWordBackward ()
  2755. {
  2756. if (_isReadOnly)
  2757. {
  2758. return;
  2759. }
  2760. SetWrapModel ();
  2761. List<Cell> currentLine = GetCurrentLine ();
  2762. _historyText.Add ([ [.. GetCurrentLine ()]], CursorPosition);
  2763. if (CurrentColumn == 0)
  2764. {
  2765. DeleteTextBackwards ();
  2766. _historyText.ReplaceLast (
  2767. [ [.. GetCurrentLine ()]],
  2768. CursorPosition,
  2769. TextEditingLineStatus.Replaced
  2770. );
  2771. UpdateWrapModel ();
  2772. return;
  2773. }
  2774. (int col, int row)? newPos = _model.WordBackward (CurrentColumn, CurrentRow, UseSameRuneTypeForWords);
  2775. if (newPos.HasValue && CurrentRow == newPos.Value.row)
  2776. {
  2777. int restCount = CurrentColumn - newPos.Value.col;
  2778. currentLine.RemoveRange (newPos.Value.col, restCount);
  2779. if (_wordWrap)
  2780. {
  2781. _wrapNeeded = true;
  2782. }
  2783. CurrentColumn = newPos.Value.col;
  2784. }
  2785. else if (newPos.HasValue)
  2786. {
  2787. int restCount;
  2788. if (newPos.Value.row == CurrentRow)
  2789. {
  2790. restCount = currentLine.Count - CurrentColumn;
  2791. currentLine.RemoveRange (CurrentColumn, restCount);
  2792. }
  2793. else
  2794. {
  2795. while (CurrentRow != newPos.Value.row)
  2796. {
  2797. restCount = currentLine.Count;
  2798. currentLine.RemoveRange (0, restCount);
  2799. CurrentRow--;
  2800. currentLine = GetCurrentLine ();
  2801. }
  2802. }
  2803. if (_wordWrap)
  2804. {
  2805. _wrapNeeded = true;
  2806. }
  2807. CurrentColumn = newPos.Value.col;
  2808. CurrentRow = newPos.Value.row;
  2809. }
  2810. _historyText.Add (
  2811. [ [.. GetCurrentLine ()]],
  2812. CursorPosition,
  2813. TextEditingLineStatus.Replaced
  2814. );
  2815. UpdateWrapModel ();
  2816. DoSetNeedsDraw (new (0, CurrentRow - _topRow, Viewport.Width, Viewport.Height));
  2817. DoNeededAction ();
  2818. }
  2819. private void KillWordForward ()
  2820. {
  2821. if (_isReadOnly)
  2822. {
  2823. return;
  2824. }
  2825. SetWrapModel ();
  2826. List<Cell> currentLine = GetCurrentLine ();
  2827. _historyText.Add ([ [.. GetCurrentLine ()]], CursorPosition);
  2828. if (currentLine.Count == 0 || CurrentColumn == currentLine.Count)
  2829. {
  2830. DeleteTextForwards ();
  2831. _historyText.ReplaceLast (
  2832. [ [.. GetCurrentLine ()]],
  2833. CursorPosition,
  2834. TextEditingLineStatus.Replaced
  2835. );
  2836. UpdateWrapModel ();
  2837. return;
  2838. }
  2839. (int col, int row)? newPos = _model.WordForward (CurrentColumn, CurrentRow, UseSameRuneTypeForWords);
  2840. var restCount = 0;
  2841. if (newPos.HasValue && CurrentRow == newPos.Value.row)
  2842. {
  2843. restCount = newPos.Value.col - CurrentColumn;
  2844. currentLine.RemoveRange (CurrentColumn, restCount);
  2845. }
  2846. else if (newPos.HasValue)
  2847. {
  2848. restCount = currentLine.Count - CurrentColumn;
  2849. currentLine.RemoveRange (CurrentColumn, restCount);
  2850. }
  2851. if (_wordWrap)
  2852. {
  2853. _wrapNeeded = true;
  2854. }
  2855. _historyText.Add (
  2856. [ [.. GetCurrentLine ()]],
  2857. CursorPosition,
  2858. TextEditingLineStatus.Replaced
  2859. );
  2860. UpdateWrapModel ();
  2861. DoSetNeedsDraw (new (0, CurrentRow - _topRow, Viewport.Width, Viewport.Height));
  2862. DoNeededAction ();
  2863. }
  2864. private void Model_LinesLoaded (object sender, EventArgs e)
  2865. {
  2866. // This call is not needed. Model_LinesLoaded gets invoked when
  2867. // model.LoadString (value) is called. LoadString is called from one place
  2868. // (Text.set) and historyText.Clear() is called immediately after.
  2869. // If this call happens, HistoryText_ChangeText will get called multiple times
  2870. // when Text is set, which is wrong.
  2871. //historyText.Clear (Text);
  2872. if (!_multiline && !IsInitialized)
  2873. {
  2874. CurrentColumn = Text.GetRuneCount ();
  2875. _leftColumn = CurrentColumn > Viewport.Width + 1 ? CurrentColumn - Viewport.Width + 1 : 0;
  2876. }
  2877. }
  2878. private void MoveBottomEnd ()
  2879. {
  2880. ResetAllTrack ();
  2881. if (_shiftSelecting && IsSelecting)
  2882. {
  2883. StopSelecting ();
  2884. }
  2885. MoveEnd ();
  2886. }
  2887. private void MoveBottomEndExtend ()
  2888. {
  2889. ResetAllTrack ();
  2890. StartSelecting ();
  2891. MoveEnd ();
  2892. }
  2893. private bool MoveDown ()
  2894. {
  2895. if (CurrentRow + 1 < _model.Count)
  2896. {
  2897. if (_columnTrack == -1)
  2898. {
  2899. _columnTrack = CurrentColumn;
  2900. }
  2901. CurrentRow++;
  2902. if (CurrentRow >= _topRow + Viewport.Height)
  2903. {
  2904. _topRow++;
  2905. SetNeedsDraw ();
  2906. }
  2907. TrackColumn ();
  2908. PositionCursor ();
  2909. }
  2910. else if (CurrentRow > Viewport.Height)
  2911. {
  2912. Adjust ();
  2913. }
  2914. else
  2915. {
  2916. return false;
  2917. }
  2918. DoNeededAction ();
  2919. return true;
  2920. }
  2921. private void MoveEndOfLine ()
  2922. {
  2923. List<Cell> currentLine = GetCurrentLine ();
  2924. CurrentColumn = currentLine.Count;
  2925. DoNeededAction ();
  2926. }
  2927. private bool MoveLeft ()
  2928. {
  2929. if (CurrentColumn > 0)
  2930. {
  2931. CurrentColumn--;
  2932. }
  2933. else
  2934. {
  2935. if (CurrentRow > 0)
  2936. {
  2937. CurrentRow--;
  2938. if (CurrentRow < _topRow)
  2939. {
  2940. _topRow--;
  2941. SetNeedsDraw ();
  2942. }
  2943. List<Cell> currentLine = GetCurrentLine ();
  2944. CurrentColumn = Math.Max (currentLine.Count - (ReadOnly ? 1 : 0), 0);
  2945. }
  2946. else
  2947. {
  2948. return false;
  2949. }
  2950. }
  2951. DoNeededAction ();
  2952. return true;
  2953. }
  2954. private void MovePageDown ()
  2955. {
  2956. int nPageDnShift = Viewport.Height - 1;
  2957. if (CurrentRow >= 0 && CurrentRow < _model.Count)
  2958. {
  2959. if (_columnTrack == -1)
  2960. {
  2961. _columnTrack = CurrentColumn;
  2962. }
  2963. CurrentRow = CurrentRow + nPageDnShift > _model.Count
  2964. ? _model.Count > 0 ? _model.Count - 1 : 0
  2965. : CurrentRow + nPageDnShift;
  2966. if (_topRow < CurrentRow - nPageDnShift)
  2967. {
  2968. _topRow = CurrentRow >= _model.Count
  2969. ? CurrentRow - nPageDnShift
  2970. : _topRow + nPageDnShift;
  2971. SetNeedsDraw ();
  2972. }
  2973. TrackColumn ();
  2974. PositionCursor ();
  2975. }
  2976. DoNeededAction ();
  2977. }
  2978. private void MovePageUp ()
  2979. {
  2980. int nPageUpShift = Viewport.Height - 1;
  2981. if (CurrentRow > 0)
  2982. {
  2983. if (_columnTrack == -1)
  2984. {
  2985. _columnTrack = CurrentColumn;
  2986. }
  2987. CurrentRow = CurrentRow - nPageUpShift < 0 ? 0 : CurrentRow - nPageUpShift;
  2988. if (CurrentRow < _topRow)
  2989. {
  2990. _topRow = _topRow - nPageUpShift < 0 ? 0 : _topRow - nPageUpShift;
  2991. SetNeedsDraw ();
  2992. }
  2993. TrackColumn ();
  2994. PositionCursor ();
  2995. }
  2996. DoNeededAction ();
  2997. }
  2998. private bool MoveRight ()
  2999. {
  3000. List<Cell> currentLine = GetCurrentLine ();
  3001. if ((ReadOnly ? CurrentColumn + 1 : CurrentColumn) < currentLine.Count)
  3002. {
  3003. CurrentColumn++;
  3004. }
  3005. else
  3006. {
  3007. if (CurrentRow + 1 < _model.Count)
  3008. {
  3009. CurrentRow++;
  3010. CurrentColumn = 0;
  3011. if (CurrentRow >= _topRow + Viewport.Height)
  3012. {
  3013. _topRow++;
  3014. SetNeedsDraw ();
  3015. }
  3016. }
  3017. else
  3018. {
  3019. return false;
  3020. }
  3021. }
  3022. DoNeededAction ();
  3023. return true;
  3024. }
  3025. private void MoveLeftStart ()
  3026. {
  3027. if (_leftColumn > 0)
  3028. {
  3029. SetNeedsDraw ();
  3030. }
  3031. CurrentColumn = 0;
  3032. _leftColumn = 0;
  3033. DoNeededAction ();
  3034. }
  3035. private void MoveTopHome ()
  3036. {
  3037. ResetAllTrack ();
  3038. if (_shiftSelecting && IsSelecting)
  3039. {
  3040. StopSelecting ();
  3041. }
  3042. MoveHome ();
  3043. }
  3044. private void MoveTopHomeExtend ()
  3045. {
  3046. ResetColumnTrack ();
  3047. StartSelecting ();
  3048. MoveHome ();
  3049. }
  3050. private bool MoveUp ()
  3051. {
  3052. if (CurrentRow > 0)
  3053. {
  3054. if (_columnTrack == -1)
  3055. {
  3056. _columnTrack = CurrentColumn;
  3057. }
  3058. CurrentRow--;
  3059. if (CurrentRow < _topRow)
  3060. {
  3061. _topRow--;
  3062. SetNeedsDraw ();
  3063. }
  3064. TrackColumn ();
  3065. PositionCursor ();
  3066. }
  3067. else
  3068. {
  3069. return false;
  3070. }
  3071. DoNeededAction ();
  3072. return true;
  3073. }
  3074. private void MoveWordBackward ()
  3075. {
  3076. (int col, int row)? newPos = _model.WordBackward (CurrentColumn, CurrentRow, UseSameRuneTypeForWords);
  3077. if (newPos.HasValue)
  3078. {
  3079. CurrentColumn = newPos.Value.col;
  3080. CurrentRow = newPos.Value.row;
  3081. }
  3082. DoNeededAction ();
  3083. }
  3084. private void MoveWordForward ()
  3085. {
  3086. (int col, int row)? newPos = _model.WordForward (CurrentColumn, CurrentRow, UseSameRuneTypeForWords);
  3087. if (newPos.HasValue)
  3088. {
  3089. CurrentColumn = newPos.Value.col;
  3090. CurrentRow = newPos.Value.row;
  3091. }
  3092. DoNeededAction ();
  3093. }
  3094. private (int width, int height) OffSetBackground ()
  3095. {
  3096. var w = 0;
  3097. var h = 0;
  3098. if (SuperView?.Viewport.Right - Viewport.Right < 0)
  3099. {
  3100. w = SuperView!.Viewport.Right - Viewport.Right - 1;
  3101. }
  3102. if (SuperView?.Viewport.Bottom - Viewport.Bottom < 0)
  3103. {
  3104. h = SuperView!.Viewport.Bottom - Viewport.Bottom - 1;
  3105. }
  3106. return (w, h);
  3107. }
  3108. private bool PointInSelection (int col, int row)
  3109. {
  3110. long start, end;
  3111. GetEncodedRegionBounds (out start, out end);
  3112. long q = ((long)(uint)row << 32) | (uint)col;
  3113. return q >= start && q <= end - 1;
  3114. }
  3115. private void ProcessAutocomplete ()
  3116. {
  3117. if (_isDrawing)
  3118. {
  3119. return;
  3120. }
  3121. if (_clickWithSelecting)
  3122. {
  3123. _clickWithSelecting = false;
  3124. return;
  3125. }
  3126. if (SelectedLength > 0)
  3127. {
  3128. return;
  3129. }
  3130. // draw autocomplete
  3131. GenerateSuggestions ();
  3132. var renderAt = new Point (
  3133. Autocomplete.Context.CursorPosition,
  3134. Autocomplete.PopupInsideContainer
  3135. ? CursorPosition.Y + 1 - TopRow
  3136. : 0
  3137. );
  3138. Autocomplete.RenderOverlay (renderAt);
  3139. }
  3140. private bool ProcessBackTab ()
  3141. {
  3142. ResetColumnTrack ();
  3143. if (!AllowsTab || _isReadOnly)
  3144. {
  3145. return false;
  3146. }
  3147. if (CurrentColumn > 0)
  3148. {
  3149. SetWrapModel ();
  3150. List<Cell> currentLine = GetCurrentLine ();
  3151. if (currentLine.Count > 0 && currentLine [CurrentColumn - 1].Rune.Value == '\t')
  3152. {
  3153. _historyText.Add (new () { new (currentLine) }, CursorPosition);
  3154. currentLine.RemoveAt (CurrentColumn - 1);
  3155. CurrentColumn--;
  3156. _historyText.Add (
  3157. new () { new (GetCurrentLine ()) },
  3158. CursorPosition,
  3159. TextEditingLineStatus.Replaced
  3160. );
  3161. }
  3162. SetNeedsDraw ();
  3163. UpdateWrapModel ();
  3164. }
  3165. DoNeededAction ();
  3166. return true;
  3167. }
  3168. private void ProcessCopy ()
  3169. {
  3170. ResetColumnTrack ();
  3171. Copy ();
  3172. }
  3173. private void ProcessCut ()
  3174. {
  3175. ResetColumnTrack ();
  3176. Cut ();
  3177. }
  3178. private void ProcessDeleteCharLeft ()
  3179. {
  3180. ResetColumnTrack ();
  3181. DeleteCharLeft ();
  3182. }
  3183. private void ProcessDeleteCharRight ()
  3184. {
  3185. ResetColumnTrack ();
  3186. DeleteCharRight ();
  3187. }
  3188. private Attribute? GetSelectedAttribute (int row, int col)
  3189. {
  3190. if (!InheritsPreviousAttribute || (Lines == 1 && GetLine (Lines).Count == 0))
  3191. {
  3192. return null;
  3193. }
  3194. List<Cell> line = GetLine (row);
  3195. int foundRow = row;
  3196. while (line.Count == 0)
  3197. {
  3198. if (foundRow == 0 && line.Count == 0)
  3199. {
  3200. return null;
  3201. }
  3202. foundRow--;
  3203. line = GetLine (foundRow);
  3204. }
  3205. int foundCol = foundRow < row ? line.Count - 1 : Math.Min (col, line.Count - 1);
  3206. Cell cell = line [foundCol];
  3207. return cell.Attribute;
  3208. }
  3209. // If InheritsPreviousScheme is enabled this method will check if the rune cell on
  3210. // the row and col location and around has a not null scheme. If it's null will set it with
  3211. // the very most previous valid scheme.
  3212. private void ProcessInheritsPreviousScheme (int row, int col)
  3213. {
  3214. if (!InheritsPreviousAttribute || (Lines == 1 && GetLine (Lines).Count == 0))
  3215. {
  3216. return;
  3217. }
  3218. List<Cell> line = GetLine (row);
  3219. List<Cell> lineToSet = line;
  3220. while (line.Count == 0)
  3221. {
  3222. if (row == 0 && line.Count == 0)
  3223. {
  3224. return;
  3225. }
  3226. row--;
  3227. line = GetLine (row);
  3228. lineToSet = line;
  3229. }
  3230. int colWithColor = Math.Max (Math.Min (col - 2, line.Count - 1), 0);
  3231. Cell cell = line [colWithColor];
  3232. int colWithoutColor = Math.Max (col - 1, 0);
  3233. Cell lineTo = lineToSet [colWithoutColor];
  3234. if (cell.Attribute is { } && colWithColor == 0 && lineTo.Attribute is { })
  3235. {
  3236. for (int r = row - 1; r > -1; r--)
  3237. {
  3238. List<Cell> l = GetLine (r);
  3239. for (int c = l.Count - 1; c > -1; c--)
  3240. {
  3241. Cell cell1 = l [c];
  3242. if (cell1.Attribute is null)
  3243. {
  3244. cell1.Attribute = cell.Attribute;
  3245. l [c] = cell1;
  3246. }
  3247. else
  3248. {
  3249. return;
  3250. }
  3251. }
  3252. }
  3253. return;
  3254. }
  3255. if (cell.Attribute is null)
  3256. {
  3257. for (int r = row; r > -1; r--)
  3258. {
  3259. List<Cell> l = GetLine (r);
  3260. colWithColor = l.FindLastIndex (
  3261. colWithColor > -1 ? colWithColor : l.Count - 1,
  3262. c => c.Attribute != null
  3263. );
  3264. if (colWithColor > -1 && l [colWithColor].Attribute is { })
  3265. {
  3266. cell = l [colWithColor];
  3267. break;
  3268. }
  3269. }
  3270. }
  3271. else
  3272. {
  3273. int cRow = row;
  3274. while (cell.Attribute is null)
  3275. {
  3276. if ((colWithColor == 0 || cell.Attribute is null) && cRow > 0)
  3277. {
  3278. line = GetLine (--cRow);
  3279. colWithColor = line.Count - 1;
  3280. cell = line [colWithColor];
  3281. }
  3282. else if (cRow == 0 && colWithColor < line.Count)
  3283. {
  3284. cell = line [colWithColor + 1];
  3285. }
  3286. }
  3287. }
  3288. if (cell.Attribute is { } && colWithColor > -1 && colWithoutColor < lineToSet.Count && lineTo.Attribute is null)
  3289. {
  3290. while (lineTo.Attribute is null)
  3291. {
  3292. lineTo.Attribute = cell.Attribute;
  3293. lineToSet [colWithoutColor] = lineTo;
  3294. colWithoutColor--;
  3295. if (colWithoutColor == -1 && row > 0)
  3296. {
  3297. lineToSet = GetLine (--row);
  3298. colWithoutColor = lineToSet.Count - 1;
  3299. }
  3300. }
  3301. }
  3302. }
  3303. private void ProcessKillWordBackward ()
  3304. {
  3305. ResetColumnTrack ();
  3306. KillWordBackward ();
  3307. }
  3308. private void ProcessKillWordForward ()
  3309. {
  3310. ResetColumnTrack ();
  3311. StopSelecting ();
  3312. KillWordForward ();
  3313. }
  3314. private void ProcessMouseClick (MouseEventArgs ev, out List<Cell> line)
  3315. {
  3316. List<Cell>? r = null;
  3317. if (_model.Count > 0)
  3318. {
  3319. int maxCursorPositionableLine = Math.Max (_model.Count - 1 - _topRow, 0);
  3320. if (Math.Max (ev.Position.Y, 0) > maxCursorPositionableLine)
  3321. {
  3322. CurrentRow = maxCursorPositionableLine + _topRow;
  3323. }
  3324. else
  3325. {
  3326. CurrentRow = Math.Max (ev.Position.Y + _topRow, 0);
  3327. }
  3328. r = GetCurrentLine ();
  3329. int idx = TextModel.GetColFromX (r, _leftColumn, Math.Max (ev.Position.X, 0), TabWidth);
  3330. if (idx - _leftColumn >= r.Count)
  3331. {
  3332. CurrentColumn = Math.Max (r.Count - _leftColumn - (ReadOnly ? 1 : 0), 0);
  3333. }
  3334. else
  3335. {
  3336. CurrentColumn = idx + _leftColumn;
  3337. }
  3338. }
  3339. line = r!;
  3340. }
  3341. private bool ProcessMoveDown ()
  3342. {
  3343. ResetContinuousFindTrack ();
  3344. if (_shiftSelecting && IsSelecting)
  3345. {
  3346. StopSelecting ();
  3347. }
  3348. return MoveDown ();
  3349. }
  3350. private void ProcessMoveDownExtend ()
  3351. {
  3352. ResetColumnTrack ();
  3353. StartSelecting ();
  3354. MoveDown ();
  3355. }
  3356. private void ProcessMoveEndOfLine ()
  3357. {
  3358. ResetAllTrack ();
  3359. if (_shiftSelecting && IsSelecting)
  3360. {
  3361. StopSelecting ();
  3362. }
  3363. MoveEndOfLine ();
  3364. }
  3365. private void ProcessMoveRightEndExtend ()
  3366. {
  3367. ResetAllTrack ();
  3368. StartSelecting ();
  3369. MoveEndOfLine ();
  3370. }
  3371. private bool ProcessMoveLeft ()
  3372. {
  3373. // if the user presses Left (without any control keys) and they are at the start of the text
  3374. if (CurrentColumn == 0 && CurrentRow == 0)
  3375. {
  3376. if (IsSelecting)
  3377. {
  3378. StopSelecting ();
  3379. return true;
  3380. }
  3381. // do not respond (this lets the key press fall through to navigation system - which usually changes focus backward)
  3382. return false;
  3383. }
  3384. ResetAllTrack ();
  3385. if (_shiftSelecting && IsSelecting)
  3386. {
  3387. StopSelecting ();
  3388. }
  3389. MoveLeft ();
  3390. return true;
  3391. }
  3392. private void ProcessMoveLeftExtend ()
  3393. {
  3394. ResetAllTrack ();
  3395. StartSelecting ();
  3396. MoveLeft ();
  3397. }
  3398. private bool ProcessMoveRight ()
  3399. {
  3400. // if the user presses Right (without any control keys)
  3401. // determine where the last cursor position in the text is
  3402. int lastRow = _model.Count - 1;
  3403. int lastCol = _model.GetLine (lastRow).Count;
  3404. // 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)
  3405. if (CurrentColumn == lastCol && CurrentRow == lastRow)
  3406. {
  3407. // Unless they have text selected
  3408. if (IsSelecting)
  3409. {
  3410. // In which case clear
  3411. StopSelecting ();
  3412. return true;
  3413. }
  3414. return false;
  3415. }
  3416. ResetAllTrack ();
  3417. if (_shiftSelecting && IsSelecting)
  3418. {
  3419. StopSelecting ();
  3420. }
  3421. MoveRight ();
  3422. return true;
  3423. }
  3424. private void ProcessMoveRightExtend ()
  3425. {
  3426. ResetAllTrack ();
  3427. StartSelecting ();
  3428. MoveRight ();
  3429. }
  3430. private void ProcessMoveLeftStart ()
  3431. {
  3432. ResetAllTrack ();
  3433. if (_shiftSelecting && IsSelecting)
  3434. {
  3435. StopSelecting ();
  3436. }
  3437. MoveLeftStart ();
  3438. }
  3439. private void ProcessMoveLeftStartExtend ()
  3440. {
  3441. ResetAllTrack ();
  3442. StartSelecting ();
  3443. MoveLeftStart ();
  3444. }
  3445. private bool ProcessMoveUp ()
  3446. {
  3447. ResetContinuousFindTrack ();
  3448. if (_shiftSelecting && IsSelecting)
  3449. {
  3450. StopSelecting ();
  3451. }
  3452. return MoveUp ();
  3453. }
  3454. private void ProcessMoveUpExtend ()
  3455. {
  3456. ResetColumnTrack ();
  3457. StartSelecting ();
  3458. MoveUp ();
  3459. }
  3460. private void ProcessMoveWordBackward ()
  3461. {
  3462. ResetAllTrack ();
  3463. if (_shiftSelecting && IsSelecting)
  3464. {
  3465. StopSelecting ();
  3466. }
  3467. MoveWordBackward ();
  3468. }
  3469. private void ProcessMoveWordBackwardExtend ()
  3470. {
  3471. ResetAllTrack ();
  3472. StartSelecting ();
  3473. MoveWordBackward ();
  3474. }
  3475. private void ProcessMoveWordForward ()
  3476. {
  3477. ResetAllTrack ();
  3478. if (_shiftSelecting && IsSelecting)
  3479. {
  3480. StopSelecting ();
  3481. }
  3482. MoveWordForward ();
  3483. }
  3484. private void ProcessMoveWordForwardExtend ()
  3485. {
  3486. ResetAllTrack ();
  3487. StartSelecting ();
  3488. MoveWordForward ();
  3489. }
  3490. private void ProcessPageDown ()
  3491. {
  3492. ResetColumnTrack ();
  3493. if (_shiftSelecting && IsSelecting)
  3494. {
  3495. StopSelecting ();
  3496. }
  3497. MovePageDown ();
  3498. }
  3499. private void ProcessPageDownExtend ()
  3500. {
  3501. ResetColumnTrack ();
  3502. StartSelecting ();
  3503. MovePageDown ();
  3504. }
  3505. private void ProcessPageUp ()
  3506. {
  3507. ResetColumnTrack ();
  3508. if (_shiftSelecting && IsSelecting)
  3509. {
  3510. StopSelecting ();
  3511. }
  3512. MovePageUp ();
  3513. }
  3514. private void ProcessPageUpExtend ()
  3515. {
  3516. ResetColumnTrack ();
  3517. StartSelecting ();
  3518. MovePageUp ();
  3519. }
  3520. private void ProcessPaste ()
  3521. {
  3522. ResetColumnTrack ();
  3523. if (_isReadOnly)
  3524. {
  3525. return;
  3526. }
  3527. Paste ();
  3528. }
  3529. private bool ProcessEnterKey (ICommandContext? commandContext)
  3530. {
  3531. ResetColumnTrack ();
  3532. if (_isReadOnly)
  3533. {
  3534. return false;
  3535. }
  3536. if (!AllowsReturn)
  3537. {
  3538. // By Default pressing ENTER should be ignored (OnAccept will return false or null). Only cancel if the
  3539. // event was fired and set Cancel = true.
  3540. return RaiseAccepting (commandContext) is null or false;
  3541. }
  3542. SetWrapModel ();
  3543. List<Cell> currentLine = GetCurrentLine ();
  3544. _historyText.Add (new () { new (currentLine) }, CursorPosition);
  3545. if (IsSelecting)
  3546. {
  3547. ClearSelectedRegion ();
  3548. currentLine = GetCurrentLine ();
  3549. }
  3550. int restCount = currentLine.Count - CurrentColumn;
  3551. List<Cell> rest = currentLine.GetRange (CurrentColumn, restCount);
  3552. currentLine.RemoveRange (CurrentColumn, restCount);
  3553. List<List<Cell>> addedLines = new () { new (currentLine) };
  3554. _model.AddLine (CurrentRow + 1, rest);
  3555. addedLines.Add (new (_model.GetLine (CurrentRow + 1)));
  3556. _historyText.Add (addedLines, CursorPosition, TextEditingLineStatus.Added);
  3557. CurrentRow++;
  3558. var fullNeedsDraw = false;
  3559. if (CurrentRow >= _topRow + Viewport.Height)
  3560. {
  3561. _topRow++;
  3562. fullNeedsDraw = true;
  3563. }
  3564. CurrentColumn = 0;
  3565. _historyText.Add (
  3566. new () { new (GetCurrentLine ()) },
  3567. CursorPosition,
  3568. TextEditingLineStatus.Replaced
  3569. );
  3570. if (!_wordWrap && CurrentColumn < _leftColumn)
  3571. {
  3572. fullNeedsDraw = true;
  3573. _leftColumn = 0;
  3574. }
  3575. if (fullNeedsDraw)
  3576. {
  3577. SetNeedsDraw ();
  3578. }
  3579. else
  3580. {
  3581. // BUGBUG: customized rect aren't supported now because the Redraw isn't using the Intersect method.
  3582. //SetNeedsDraw (new (0, currentRow - topRow, 2, Viewport.Height));
  3583. SetNeedsDraw ();
  3584. }
  3585. UpdateWrapModel ();
  3586. DoNeededAction ();
  3587. OnContentsChanged ();
  3588. return true;
  3589. }
  3590. private void ProcessSelectAll ()
  3591. {
  3592. ResetColumnTrack ();
  3593. SelectAll ();
  3594. }
  3595. private void ProcessSetOverwrite ()
  3596. {
  3597. ResetColumnTrack ();
  3598. SetOverwrite (!Used);
  3599. }
  3600. private bool ProcessTab ()
  3601. {
  3602. ResetColumnTrack ();
  3603. if (!AllowsTab || _isReadOnly)
  3604. {
  3605. return false;
  3606. }
  3607. InsertText (new Key ((KeyCode)'\t'));
  3608. DoNeededAction ();
  3609. return true;
  3610. }
  3611. private void ResetAllTrack ()
  3612. {
  3613. // Handle some state here - whether the last command was a kill
  3614. // operation and the column tracking (up/down)
  3615. _lastWasKill = false;
  3616. _columnTrack = -1;
  3617. _continuousFind = false;
  3618. }
  3619. private void ResetColumnTrack ()
  3620. {
  3621. // Handle some state here - whether the last command was a kill
  3622. // operation and the column tracking (up/down)
  3623. _lastWasKill = false;
  3624. _columnTrack = -1;
  3625. }
  3626. private void ResetContinuousFind ()
  3627. {
  3628. if (!_continuousFind)
  3629. {
  3630. int col = IsSelecting ? _selectionStartColumn : CurrentColumn;
  3631. int row = IsSelecting ? _selectionStartRow : CurrentRow;
  3632. _model.ResetContinuousFind (new (col, row));
  3633. }
  3634. }
  3635. private void ResetContinuousFindTrack ()
  3636. {
  3637. // Handle some state here - whether the last command was a kill
  3638. // operation and the column tracking (up/down)
  3639. _lastWasKill = false;
  3640. _continuousFind = false;
  3641. }
  3642. private void ResetPosition ()
  3643. {
  3644. _topRow = _leftColumn = CurrentRow = CurrentColumn = 0;
  3645. StopSelecting ();
  3646. }
  3647. private void SetClipboard (string text)
  3648. {
  3649. if (text is { })
  3650. {
  3651. Clipboard.Contents = text;
  3652. }
  3653. }
  3654. private bool SetFoundText (
  3655. string text,
  3656. (Point current, bool found) foundPos,
  3657. string? textToReplace = null,
  3658. bool replace = false,
  3659. bool replaceAll = false
  3660. )
  3661. {
  3662. if (foundPos.found)
  3663. {
  3664. StartSelecting ();
  3665. _selectionStartColumn = foundPos.current.X;
  3666. _selectionStartRow = foundPos.current.Y;
  3667. if (!replaceAll)
  3668. {
  3669. CurrentColumn = _selectionStartColumn + text.GetRuneCount ();
  3670. }
  3671. else
  3672. {
  3673. CurrentColumn = _selectionStartColumn + textToReplace!.GetRuneCount ();
  3674. }
  3675. CurrentRow = foundPos.current.Y;
  3676. if (!_isReadOnly && replace)
  3677. {
  3678. Adjust ();
  3679. ClearSelectedRegion ();
  3680. InsertAllText (textToReplace!);
  3681. StartSelecting ();
  3682. _selectionStartColumn = CurrentColumn - textToReplace!.GetRuneCount ();
  3683. }
  3684. else
  3685. {
  3686. UpdateWrapModel ();
  3687. SetNeedsDraw ();
  3688. Adjust ();
  3689. }
  3690. _continuousFind = true;
  3691. return foundPos.found;
  3692. }
  3693. UpdateWrapModel ();
  3694. _continuousFind = false;
  3695. return foundPos.found;
  3696. }
  3697. private void SetOverwrite (bool overwrite)
  3698. {
  3699. Used = overwrite;
  3700. SetNeedsDraw ();
  3701. DoNeededAction ();
  3702. }
  3703. private void SetValidUsedColor (Attribute? attribute)
  3704. {
  3705. // BUGBUG: (v2 truecolor) This code depends on 8-bit color names; disabling for now
  3706. //if ((scheme!.HotNormal.Foreground & scheme.Focus.Background) == scheme.Focus.Foreground) {
  3707. SetAttribute (new (attribute!.Value.Background, attribute!.Value.Foreground, attribute!.Value.Style));
  3708. }
  3709. /// <summary>Restore from original model.</summary>
  3710. private void SetWrapModel ([CallerMemberName] string? caller = null)
  3711. {
  3712. if (_currentCaller is { })
  3713. {
  3714. return;
  3715. }
  3716. if (_wordWrap)
  3717. {
  3718. _currentCaller = caller;
  3719. CurrentColumn = _wrapManager!.GetModelColFromWrappedLines (CurrentRow, CurrentColumn);
  3720. CurrentRow = _wrapManager.GetModelLineFromWrappedLines (CurrentRow);
  3721. _selectionStartColumn =
  3722. _wrapManager.GetModelColFromWrappedLines (_selectionStartRow, _selectionStartColumn);
  3723. _selectionStartRow = _wrapManager.GetModelLineFromWrappedLines (_selectionStartRow);
  3724. _model = _wrapManager.Model;
  3725. }
  3726. }
  3727. private void ShowContextMenu (Point? mousePosition)
  3728. {
  3729. if (!Equals (_currentCulture, Thread.CurrentThread.CurrentUICulture))
  3730. {
  3731. _currentCulture = Thread.CurrentThread.CurrentUICulture;
  3732. }
  3733. if (mousePosition is null)
  3734. {
  3735. mousePosition = ViewportToScreen (new Point (CursorPosition.X, CursorPosition.Y));
  3736. }
  3737. ContextMenu?.MakeVisible (mousePosition);
  3738. }
  3739. private void StartSelecting ()
  3740. {
  3741. if (_shiftSelecting && IsSelecting)
  3742. {
  3743. return;
  3744. }
  3745. _shiftSelecting = true;
  3746. IsSelecting = true;
  3747. _selectionStartColumn = CurrentColumn;
  3748. _selectionStartRow = CurrentRow;
  3749. }
  3750. private void StopSelecting ()
  3751. {
  3752. if (IsSelecting)
  3753. {
  3754. SetNeedsDraw ();
  3755. }
  3756. _shiftSelecting = false;
  3757. IsSelecting = false;
  3758. _isButtonShift = false;
  3759. }
  3760. private string StringFromRunes (List<Cell> cells)
  3761. {
  3762. if (cells is null)
  3763. {
  3764. throw new ArgumentNullException (nameof (cells));
  3765. }
  3766. var size = 0;
  3767. foreach (Cell cell in cells)
  3768. {
  3769. size += cell.Rune.GetEncodingLength ();
  3770. }
  3771. var encoded = new byte [size];
  3772. var offset = 0;
  3773. foreach (Cell cell in cells)
  3774. {
  3775. offset += cell.Rune.Encode (encoded, offset);
  3776. }
  3777. return StringExtensions.ToString (encoded);
  3778. }
  3779. private void TextView_SuperViewChanged (object sender, SuperViewChangedEventArgs e)
  3780. {
  3781. if (e.SuperView is { })
  3782. {
  3783. if (Autocomplete.HostControl is null)
  3784. {
  3785. Autocomplete.HostControl = this;
  3786. }
  3787. }
  3788. else
  3789. {
  3790. Autocomplete.HostControl = null;
  3791. }
  3792. }
  3793. private void TextView_Initialized (object sender, EventArgs e)
  3794. {
  3795. if (Autocomplete.HostControl is null)
  3796. {
  3797. Autocomplete.HostControl = this;
  3798. }
  3799. OnContentsChanged ();
  3800. }
  3801. private void TextView_LayoutComplete (object? sender, LayoutEventArgs e)
  3802. {
  3803. WrapTextModel ();
  3804. Adjust ();
  3805. }
  3806. private void ToggleSelecting ()
  3807. {
  3808. ResetColumnTrack ();
  3809. IsSelecting = !IsSelecting;
  3810. _selectionStartColumn = CurrentColumn;
  3811. _selectionStartRow = CurrentRow;
  3812. }
  3813. // Tries to snap the cursor to the tracking column
  3814. private void TrackColumn ()
  3815. {
  3816. // Now track the column
  3817. List<Cell> line = GetCurrentLine ();
  3818. if (line.Count < _columnTrack)
  3819. {
  3820. CurrentColumn = line.Count;
  3821. }
  3822. else if (_columnTrack != -1)
  3823. {
  3824. CurrentColumn = _columnTrack;
  3825. }
  3826. else if (CurrentColumn > line.Count)
  3827. {
  3828. CurrentColumn = line.Count;
  3829. }
  3830. Adjust ();
  3831. }
  3832. /// <summary>Update the original model.</summary>
  3833. private void UpdateWrapModel ([CallerMemberName] string? caller = null)
  3834. {
  3835. if (_currentCaller is { } && _currentCaller != caller)
  3836. {
  3837. return;
  3838. }
  3839. if (_wordWrap)
  3840. {
  3841. _currentCaller = null;
  3842. _wrapManager!.UpdateModel (
  3843. _model,
  3844. out int nRow,
  3845. out int nCol,
  3846. out int nStartRow,
  3847. out int nStartCol,
  3848. CurrentRow,
  3849. CurrentColumn,
  3850. _selectionStartRow,
  3851. _selectionStartColumn,
  3852. true
  3853. );
  3854. CurrentRow = nRow;
  3855. CurrentColumn = nCol;
  3856. _selectionStartRow = nStartRow;
  3857. _selectionStartColumn = nStartCol;
  3858. _wrapNeeded = true;
  3859. SetNeedsDraw ();
  3860. }
  3861. if (_currentCaller is { })
  3862. {
  3863. throw new InvalidOperationException (
  3864. $"WordWrap settings was changed after the {_currentCaller} call."
  3865. );
  3866. }
  3867. }
  3868. private void WrapTextModel ()
  3869. {
  3870. if (_wordWrap && _wrapManager is { })
  3871. {
  3872. _model = _wrapManager.WrapModel (
  3873. Math.Max (Viewport.Width - (ReadOnly ? 0 : 1), 0), // For the cursor on the last column of a line
  3874. out int nRow,
  3875. out int nCol,
  3876. out int nStartRow,
  3877. out int nStartCol,
  3878. CurrentRow,
  3879. CurrentColumn,
  3880. _selectionStartRow,
  3881. _selectionStartColumn,
  3882. _tabWidth
  3883. );
  3884. CurrentRow = nRow;
  3885. CurrentColumn = nCol;
  3886. _selectionStartRow = nStartRow;
  3887. _selectionStartColumn = nStartCol;
  3888. SetNeedsDraw ();
  3889. }
  3890. }
  3891. /// <inheritdoc />
  3892. public bool EnableForDesign ()
  3893. {
  3894. Text = """
  3895. TextView provides a fully featured multi-line text editor.
  3896. It supports word wrap and history for undo.
  3897. """;
  3898. return true;
  3899. }
  3900. /// <inheritdoc/>
  3901. protected override void Dispose (bool disposing)
  3902. {
  3903. if (disposing && ContextMenu is { })
  3904. {
  3905. ContextMenu.Visible = false;
  3906. ContextMenu.Dispose ();
  3907. ContextMenu = null;
  3908. }
  3909. base.Dispose (disposing);
  3910. }
  3911. }
  3912. /// <summary>
  3913. /// Renders an overlay on another view at a given point that allows selecting from a range of 'autocomplete'
  3914. /// options. An implementation on a TextView.
  3915. /// </summary>
  3916. public class TextViewAutocomplete : PopupAutocomplete
  3917. {
  3918. /// <inheritdoc/>
  3919. protected override void DeleteTextBackwards () { ((TextView)HostControl!).DeleteCharLeft (); }
  3920. /// <inheritdoc/>
  3921. protected override void InsertText (string accepted) { ((TextView)HostControl!).InsertText (accepted); }
  3922. /// <inheritdoc/>
  3923. protected override void SetCursorPosition (int column)
  3924. {
  3925. ((TextView)HostControl!).CursorPosition =
  3926. new (column, ((TextView)HostControl).CurrentRow);
  3927. }
  3928. }