TextView.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. //
  2. // TextView.cs: multi-line text editing
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. //
  8. // TODO:
  9. // Attributed text on spans
  10. // Replace insertion with Insert method
  11. // String accumulation (Control-k, control-k is not preserving the last new line, see StringToRunes
  12. // Alt-D, Alt-Backspace
  13. // API to set the cursor position
  14. // API to scroll to a particular place
  15. // keybindings to go to top/bottom
  16. // public API to insert, remove ranges
  17. // Add word forward/word backwards commands
  18. using System;
  19. using System.Collections.Generic;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Text;
  23. using NStack;
  24. namespace Terminal.Gui {
  25. class TextModel {
  26. List<List<Rune>> lines;
  27. public bool LoadFile (string file)
  28. {
  29. if (file == null)
  30. throw new ArgumentNullException (nameof (file));
  31. try {
  32. var stream = File.OpenRead (file);
  33. if (stream == null)
  34. return false;
  35. } catch {
  36. return false;
  37. }
  38. LoadStream (File.OpenRead (file));
  39. return true;
  40. }
  41. // Turns the ustring into runes, this does not split the
  42. // contents on a newline if it is present.
  43. static List<Rune> ToRunes (ustring str)
  44. {
  45. List<Rune> runes = new List<Rune> ();
  46. foreach (var x in str.ToRunes ()) {
  47. runes.Add (x);
  48. }
  49. return runes;
  50. }
  51. // Splits a string into a List that contains a List<Rune> for each line
  52. public static List<List<Rune>> StringToRunes (ustring content)
  53. {
  54. var lines = new List<List<Rune>> ();
  55. int start = 0, i = 0;
  56. for (; i < content.Length; i++) {
  57. if (content [i] == 10) {
  58. if (i - start > 0)
  59. lines.Add (ToRunes (content [start, i]));
  60. else
  61. lines.Add (ToRunes (ustring.Empty));
  62. start = i + 1;
  63. }
  64. }
  65. if (i - start >= 0)
  66. lines.Add (ToRunes (content [start, null]));
  67. return lines;
  68. }
  69. void Append (List<byte> line)
  70. {
  71. var str = ustring.Make (line.ToArray ());
  72. lines.Add (ToRunes (str));
  73. }
  74. public void LoadStream (Stream input)
  75. {
  76. if (input == null)
  77. throw new ArgumentNullException (nameof (input));
  78. lines = new List<List<Rune>> ();
  79. var buff = new BufferedStream (input);
  80. int v;
  81. var line = new List<byte> ();
  82. while ((v = buff.ReadByte ()) != -1) {
  83. if (v == 10) {
  84. Append (line);
  85. line.Clear ();
  86. continue;
  87. }
  88. line.Add ((byte)v);
  89. }
  90. if (line.Count > 0)
  91. Append (line);
  92. }
  93. public void LoadString (ustring content)
  94. {
  95. lines = StringToRunes (content);
  96. }
  97. public override string ToString ()
  98. {
  99. var sb = new StringBuilder ();
  100. foreach (var line in lines) {
  101. sb.Append (line);
  102. sb.AppendLine ();
  103. }
  104. return sb.ToString ();
  105. }
  106. public int Count => lines.Count;
  107. public List<Rune> GetLine (int line) => lines [line];
  108. public void AddLine (int pos, List<Rune> runes)
  109. {
  110. lines.Insert (pos, runes);
  111. }
  112. public void RemoveLine (int pos)
  113. {
  114. lines.RemoveAt (pos);
  115. }
  116. }
  117. /// <summary>
  118. /// Multi-line text editing view
  119. /// </summary>
  120. /// <remarks>
  121. /// <para>
  122. /// The text view provides a multi-line text view. Users interact
  123. /// with it with the standard Emacs commands for movement or the arrow
  124. /// keys.
  125. /// </para>
  126. /// <list type="table">
  127. /// <listheader>
  128. /// <term>Shortcut</term>
  129. /// <description>Action performed</description>
  130. /// </listheader>
  131. /// <item>
  132. /// <term>Left cursor, Control-b</term>
  133. /// <description>
  134. /// Moves the editing point left.
  135. /// </description>
  136. /// </item>
  137. /// <item>
  138. /// <term>Right cursor, Control-f</term>
  139. /// <description>
  140. /// Moves the editing point right.
  141. /// </description>
  142. /// </item>
  143. /// <item>
  144. /// <term>Alt-b</term>
  145. /// <description>
  146. /// Moves one word back.
  147. /// </description>
  148. /// </item>
  149. /// <item>
  150. /// <term>Alt-f</term>
  151. /// <description>
  152. /// Moves one word forward.
  153. /// </description>
  154. /// </item>
  155. /// <item>
  156. /// <term>Up cursor, Control-p</term>
  157. /// <description>
  158. /// Moves the editing point one line up.
  159. /// </description>
  160. /// </item>
  161. /// <item>
  162. /// <term>Down cursor, Control-n</term>
  163. /// <description>
  164. /// Moves the editing point one line down
  165. /// </description>
  166. /// </item>
  167. /// <item>
  168. /// <term>Home key, Control-a</term>
  169. /// <description>
  170. /// Moves the cursor to the beginning of the line.
  171. /// </description>
  172. /// </item>
  173. /// <item>
  174. /// <term>End key, Control-e</term>
  175. /// <description>
  176. /// Moves the cursor to the end of the line.
  177. /// </description>
  178. /// </item>
  179. /// <item>
  180. /// <term>Delete, Control-d</term>
  181. /// <description>
  182. /// Deletes the character in front of the cursor.
  183. /// </description>
  184. /// </item>
  185. /// <item>
  186. /// <term>Backspace</term>
  187. /// <description>
  188. /// Deletes the character behind the cursor.
  189. /// </description>
  190. /// </item>
  191. /// <item>
  192. /// <term>Control-k</term>
  193. /// <description>
  194. /// Deletes the text until the end of the line and replaces the kill buffer
  195. /// with the deleted text. You can paste this text in a different place by
  196. /// using Control-y.
  197. /// </description>
  198. /// </item>
  199. /// <item>
  200. /// <item>
  201. /// <term>Control-y</term>
  202. /// <description>
  203. /// Pastes the content of the kill ring into the current position.
  204. /// </description>
  205. /// </item>
  206. /// <item>
  207. /// <term>Alt-d</term>
  208. /// <description>
  209. /// Deletes the word above the cursor and adds it to the kill ring. You
  210. /// can paste the contents of the kill ring with Control-y.
  211. /// </description>
  212. /// </item>
  213. /// <item>
  214. /// <term>Control-q</term>
  215. /// <description>
  216. /// Quotes the next input character, to prevent the normal processing of
  217. /// key handling to take place.
  218. /// </description>
  219. /// </item>
  220. /// </list>
  221. /// </remarks>
  222. public class TextView : View {
  223. TextModel model = new TextModel ();
  224. int topRow;
  225. int leftColumn;
  226. int currentRow;
  227. int currentColumn;
  228. int selectionStartColumn, selectionStartRow;
  229. bool selecting;
  230. //bool used;
  231. #if false
  232. /// <summary>
  233. /// Changed event, raised when the text has clicked.
  234. /// </summary>
  235. /// <remarks>
  236. /// Client code can hook up to this event, it is
  237. /// raised when the text in the entry changes.
  238. /// </remarks>
  239. public event EventHandler Changed;
  240. #endif
  241. /// <summary>
  242. /// Public constructor, creates a view on the specfied area
  243. /// </summary>
  244. /// <remarks>
  245. /// </remarks>
  246. public TextView (Rect frame) : base (frame)
  247. {
  248. CanFocus = true;
  249. }
  250. void ResetPosition ()
  251. {
  252. topRow = leftColumn = currentRow = currentColumn = 0;
  253. }
  254. /// <summary>
  255. /// Sets or gets the text in the entry.
  256. /// </summary>
  257. /// <remarks>
  258. /// </remarks>
  259. public ustring Text {
  260. get {
  261. return model.ToString ();
  262. }
  263. set {
  264. ResetPosition ();
  265. model.LoadString (value);
  266. SetNeedsDisplay ();
  267. }
  268. }
  269. /// <summary>
  270. /// The current cursor row.
  271. /// </summary>
  272. public int CurrentRow => currentRow;
  273. /// <summary>
  274. /// Gets the cursor column.
  275. /// </summary>
  276. /// <value>The cursor column.</value>
  277. public int CurrentColumn => currentColumn;
  278. /// <summary>
  279. /// Positions the cursor on the current row and column
  280. /// </summary>
  281. public override void PositionCursor ()
  282. {
  283. if (selecting) {
  284. var minRow = Math.Min (Math.Max (Math.Min (selectionStartRow, currentRow)-topRow, 0), Frame.Height);
  285. var maxRow = Math.Min (Math.Max (Math.Max (selectionStartRow, currentRow) - topRow, 0), Frame.Height);
  286. SetNeedsDisplay (new Rect (0, minRow, Frame.Width, maxRow));
  287. }
  288. Move (CurrentColumn - leftColumn, CurrentRow - topRow);
  289. }
  290. void ClearRegion (int left, int top, int right, int bottom)
  291. {
  292. for (int row = top; row < bottom; row++) {
  293. Move (left, row);
  294. for (int col = left; col < right; col++)
  295. AddRune (col, row, ' ');
  296. }
  297. }
  298. void ColorNormal ()
  299. {
  300. Driver.SetAttribute (ColorScheme.Normal);
  301. }
  302. void ColorSelection ()
  303. {
  304. if (HasFocus)
  305. Driver.SetAttribute (ColorScheme.Focus);
  306. else
  307. Driver.SetAttribute (ColorScheme.Normal);
  308. }
  309. // Returns an encoded region start..end (top 32 bits are the row, low32 the column)
  310. void GetEncodedRegionBounds (out long start, out long end)
  311. {
  312. long selection = ((long)(uint)selectionStartRow << 32) | (uint)selectionStartColumn;
  313. long point = ((long)(uint)currentRow << 32) | (uint)currentColumn;
  314. if (selection > point) {
  315. start = point;
  316. end = selection;
  317. } else {
  318. start = selection;
  319. end = point;
  320. }
  321. }
  322. bool PointInSelection (int col, int row)
  323. {
  324. long start, end;
  325. GetEncodedRegionBounds (out start, out end);
  326. var q = ((long)(uint)row << 32) | (uint)col;
  327. return q >= start && q <= end;
  328. }
  329. //
  330. // Returns a ustring with the text in the selected
  331. // region.
  332. //
  333. ustring GetRegion ()
  334. {
  335. long start, end;
  336. GetEncodedRegionBounds (out start, out end);
  337. int startRow = (int)(start >> 32);
  338. var maxrow = ((int)(end >> 32));
  339. int startCol = (int)(start & 0xffffffff);
  340. var endCol = (int)(end & 0xffffffff);
  341. var line = model.GetLine (startRow);
  342. if (startRow == maxrow)
  343. return StringFromRunes (line.GetRange (startCol, endCol));
  344. ustring res = StringFromRunes (line.GetRange (startCol, line.Count - startCol));
  345. for (int row = startRow+1; row < maxrow; row++) {
  346. res = res + ustring.Make (10) + StringFromRunes (model.GetLine (row));
  347. }
  348. line = model.GetLine (maxrow);
  349. res = res + ustring.Make (10) + StringFromRunes (line.GetRange (0, endCol));
  350. return res;
  351. }
  352. //
  353. // Clears the contents of the selected region
  354. //
  355. void ClearRegion ()
  356. {
  357. long start, end;
  358. long currentEncoded = ((long)(uint)currentRow << 32) | (uint)currentColumn;
  359. GetEncodedRegionBounds (out start, out end);
  360. int startRow = (int)(start >> 32);
  361. var maxrow = ((int)(end >> 32));
  362. int startCol = (int)(start & 0xffffffff);
  363. var endCol = (int)(end & 0xffffffff);
  364. var line = model.GetLine (startRow);
  365. if (startRow == maxrow) {
  366. line.RemoveRange (startCol, endCol - startCol);
  367. currentColumn = startCol;
  368. SetNeedsDisplay (new Rect (0, startRow - topRow, Frame.Width, startRow - topRow + 1));
  369. return;
  370. }
  371. line.RemoveRange (startCol, line.Count - startCol);
  372. var line2 = model.GetLine (maxrow);
  373. line.AddRange (line2.Skip (endCol));
  374. for (int row = startRow + 1; row <= maxrow; row++) {
  375. model.RemoveLine (startRow+1);
  376. }
  377. if (currentEncoded == end) {
  378. currentRow -= maxrow - (startRow);
  379. }
  380. currentColumn = startCol;
  381. SetNeedsDisplay ();
  382. }
  383. /// <summary>
  384. /// Redraw the text editor region
  385. /// </summary>
  386. /// <param name="region">The region to redraw.</param>
  387. public override void Redraw (Rect region)
  388. {
  389. ColorNormal ();
  390. int bottom = region.Bottom;
  391. int right = region.Right;
  392. for (int row = region.Top; row < bottom; row++) {
  393. int textLine = topRow + row;
  394. if (textLine >= model.Count) {
  395. ColorNormal ();
  396. ClearRegion (region.Left, row, region.Right, row + 1);
  397. continue;
  398. }
  399. var line = model.GetLine (textLine);
  400. int lineRuneCount = line.Count;
  401. if (line.Count < region.Left){
  402. ClearRegion (region.Left, row, region.Right, row + 1);
  403. continue;
  404. }
  405. Move (region.Left, row);
  406. for (int col = region.Left; col < right; col++) {
  407. var lineCol = leftColumn + col;
  408. var rune = lineCol >= lineRuneCount ? ' ' : line [lineCol];
  409. if (selecting && PointInSelection (col, row))
  410. ColorSelection ();
  411. else
  412. ColorNormal ();
  413. AddRune (col, row, rune);
  414. }
  415. }
  416. PositionCursor ();
  417. }
  418. public override bool CanFocus {
  419. get => true;
  420. set { base.CanFocus = value; }
  421. }
  422. void SetClipboard (ustring text)
  423. {
  424. Clipboard.Contents = text;
  425. }
  426. void AppendClipboard (ustring text)
  427. {
  428. Clipboard.Contents = Clipboard.Contents + text;
  429. }
  430. void Insert (Rune rune)
  431. {
  432. var line = GetCurrentLine ();
  433. line.Insert (currentColumn, rune);
  434. var prow = currentRow - topRow;
  435. SetNeedsDisplay (new Rect (0, prow, Frame.Width, prow + 1));
  436. }
  437. ustring StringFromRunes (List<Rune> runes)
  438. {
  439. if (runes == null)
  440. throw new ArgumentNullException (nameof (runes));
  441. int size = 0;
  442. foreach (var rune in runes) {
  443. size += Utf8.RuneLen (rune);
  444. }
  445. var encoded = new byte [size];
  446. int offset = 0;
  447. foreach (var rune in runes) {
  448. offset += Utf8.EncodeRune (rune, encoded, offset);
  449. }
  450. return ustring.Make (encoded);
  451. }
  452. List<Rune> GetCurrentLine () => model.GetLine (currentRow);
  453. void InsertText (ustring text)
  454. {
  455. var lines = TextModel.StringToRunes (text);
  456. if (lines.Count == 0)
  457. return;
  458. var line = GetCurrentLine ();
  459. // Optmize single line
  460. if (lines.Count == 1) {
  461. line.InsertRange (currentColumn, lines [0]);
  462. currentColumn += lines [0].Count;
  463. if (currentColumn - leftColumn > Frame.Width)
  464. leftColumn = currentColumn - Frame.Width + 1;
  465. SetNeedsDisplay (new Rect (0, currentRow - topRow, Frame.Width, currentRow - topRow + 1));
  466. return;
  467. }
  468. // Keep a copy of the rest of the line
  469. var restCount = line.Count - currentColumn;
  470. var rest = line.GetRange (currentColumn, restCount);
  471. line.RemoveRange (currentColumn, restCount);
  472. // First line is inserted at the current location, the rest is appended
  473. line.InsertRange (currentColumn, lines [0]);
  474. for (int i = 1; i < lines.Count; i++)
  475. model.AddLine (currentRow + i, lines [i]);
  476. var last = model.GetLine (currentRow + lines.Count-1);
  477. var lastp = last.Count;
  478. last.InsertRange (last.Count, rest);
  479. // Now adjjust column and row positions
  480. currentRow += lines.Count-1;
  481. currentColumn = lastp;
  482. if (currentRow - topRow > Frame.Height) {
  483. topRow = currentRow - Frame.Height + 1;
  484. if (topRow < 0)
  485. topRow = 0;
  486. }
  487. if (currentColumn < leftColumn)
  488. leftColumn = currentColumn;
  489. if (currentColumn-leftColumn >= Frame.Width)
  490. leftColumn = currentColumn - Frame.Width + 1;
  491. SetNeedsDisplay ();
  492. }
  493. // The column we are tracking, or -1 if we are not tracking any column
  494. int columnTrack = -1;
  495. // Tries to snap the cursor to the tracking column
  496. void TrackColumn ()
  497. {
  498. // Now track the column
  499. var line = GetCurrentLine ();
  500. if (line.Count < columnTrack)
  501. currentColumn = line.Count;
  502. else if (columnTrack != -1)
  503. currentColumn = columnTrack;
  504. else if (currentColumn > line.Count)
  505. currentColumn = line.Count;
  506. if (currentColumn < leftColumn) {
  507. leftColumn = currentColumn;
  508. SetNeedsDisplay ();
  509. }
  510. if (currentColumn - leftColumn > Frame.Width) {
  511. leftColumn = currentColumn - Frame.Width + 1;
  512. SetNeedsDisplay ();
  513. }
  514. }
  515. bool lastWasKill;
  516. public override bool ProcessKey (KeyEvent kb)
  517. {
  518. int restCount;
  519. List<Rune> rest;
  520. // Handle some state here - whether the last command was a kill
  521. // operation and the column tracking (up/down)
  522. switch (kb.Key) {
  523. case Key.ControlN:
  524. case Key.CursorDown:
  525. case Key.ControlP:
  526. case Key.CursorUp:
  527. lastWasKill = false;
  528. break;
  529. case Key.ControlK:
  530. break;
  531. default:
  532. lastWasKill = false;
  533. columnTrack = -1;
  534. break;
  535. }
  536. // Dispatch the command.
  537. switch (kb.Key) {
  538. case Key.ControlN:
  539. case Key.CursorDown:
  540. if (currentRow + 1 < model.Count) {
  541. if (columnTrack == -1)
  542. columnTrack = currentColumn;
  543. currentRow++;
  544. if (currentRow >= topRow + Frame.Height) {
  545. topRow++;
  546. SetNeedsDisplay ();
  547. }
  548. TrackColumn ();
  549. PositionCursor ();
  550. }
  551. break;
  552. case Key.ControlP:
  553. case Key.CursorUp:
  554. if (currentRow > 0) {
  555. if (columnTrack == -1)
  556. columnTrack = currentColumn;
  557. currentRow--;
  558. if (currentRow < topRow) {
  559. topRow--;
  560. SetNeedsDisplay ();
  561. }
  562. TrackColumn ();
  563. PositionCursor ();
  564. }
  565. break;
  566. case Key.ControlF:
  567. case Key.CursorRight:
  568. var currentLine = GetCurrentLine ();
  569. if (currentColumn < currentLine.Count) {
  570. currentColumn++;
  571. if (currentColumn >= leftColumn + Frame.Width) {
  572. leftColumn++;
  573. SetNeedsDisplay ();
  574. }
  575. PositionCursor ();
  576. } else {
  577. if (currentRow + 1 < model.Count) {
  578. currentRow++;
  579. currentColumn = 0;
  580. leftColumn = 0;
  581. if (currentRow >= topRow + Frame.Height) {
  582. topRow++;
  583. }
  584. SetNeedsDisplay ();
  585. PositionCursor ();
  586. }
  587. break;
  588. }
  589. break;
  590. case Key.ControlB:
  591. case Key.CursorLeft:
  592. if (currentColumn > 0) {
  593. currentColumn--;
  594. if (currentColumn < leftColumn) {
  595. leftColumn--;
  596. SetNeedsDisplay ();
  597. }
  598. PositionCursor ();
  599. } else {
  600. if (currentRow > 0) {
  601. currentRow--;
  602. if (currentRow < topRow) {
  603. topRow--;
  604. }
  605. currentLine = GetCurrentLine ();
  606. currentColumn = currentLine.Count;
  607. int prev = leftColumn;
  608. leftColumn = currentColumn - Frame.Width + 1;
  609. if (leftColumn < 0)
  610. leftColumn = 0;
  611. if (prev != leftColumn)
  612. SetNeedsDisplay ();
  613. PositionCursor ();
  614. }
  615. }
  616. break;
  617. case Key.Delete:
  618. case Key.Backspace:
  619. if (currentColumn > 0) {
  620. // Delete backwards
  621. currentLine = GetCurrentLine ();
  622. currentLine.RemoveAt (currentColumn - 1);
  623. currentColumn--;
  624. if (currentColumn < leftColumn) {
  625. leftColumn--;
  626. SetNeedsDisplay ();
  627. } else
  628. SetNeedsDisplay (new Rect (0, currentRow - topRow, 1, Frame.Width));
  629. } else {
  630. // Merges the current line with the previous one.
  631. if (currentRow == 0)
  632. return true;
  633. var prowIdx = currentRow - 1;
  634. var prevRow = model.GetLine (prowIdx);
  635. var prevCount = prevRow.Count;
  636. model.GetLine (prowIdx).AddRange (GetCurrentLine ());
  637. currentRow--;
  638. currentColumn = prevCount;
  639. leftColumn = currentColumn - Frame.Width + 1;
  640. if (leftColumn < 0)
  641. leftColumn = 0;
  642. SetNeedsDisplay ();
  643. }
  644. break;
  645. // Home, C-A
  646. case Key.Home:
  647. case Key.ControlA:
  648. currentColumn = 0;
  649. if (currentColumn < leftColumn) {
  650. leftColumn = 0;
  651. SetNeedsDisplay ();
  652. } else
  653. PositionCursor ();
  654. break;
  655. case Key.ControlD: // Delete
  656. currentLine = GetCurrentLine ();
  657. if (currentColumn == currentLine.Count) {
  658. if (currentRow + 1 == model.Count)
  659. break;
  660. var nextLine = model.GetLine (currentRow + 1);
  661. currentLine.AddRange (nextLine);
  662. model.RemoveLine (currentRow + 1);
  663. var sr = currentRow - topRow;
  664. SetNeedsDisplay (new Rect (0, sr, Frame.Width, sr + 1));
  665. } else {
  666. currentLine.RemoveAt (currentColumn);
  667. var r = currentRow - topRow;
  668. SetNeedsDisplay (new Rect (currentColumn - leftColumn, r, Frame.Width, r + 1));
  669. }
  670. break;
  671. case Key.ControlE: // End
  672. currentLine = GetCurrentLine ();
  673. currentColumn = currentLine.Count;
  674. int pcol = leftColumn;
  675. leftColumn = currentColumn - Frame.Width + 1;
  676. if (leftColumn < 0)
  677. leftColumn = 0;
  678. if (pcol != leftColumn)
  679. SetNeedsDisplay ();
  680. PositionCursor ();
  681. break;
  682. case Key.ControlK: // kill-to-end
  683. currentLine = GetCurrentLine ();
  684. if (currentLine.Count == 0) {
  685. model.RemoveLine (currentRow);
  686. var val = ustring.Make ('\n');
  687. if (lastWasKill)
  688. AppendClipboard (val);
  689. else
  690. SetClipboard (val);
  691. } else {
  692. restCount = currentLine.Count - currentColumn;
  693. rest = currentLine.GetRange (currentColumn, restCount);
  694. var val = StringFromRunes (rest);
  695. if (lastWasKill)
  696. AppendClipboard (val);
  697. else
  698. SetClipboard (val);
  699. currentLine.RemoveRange (currentColumn, restCount);
  700. }
  701. SetNeedsDisplay (new Rect (0, currentRow - topRow, Frame.Width, Frame.Height));
  702. lastWasKill = true;
  703. break;
  704. case Key.ControlY: // Control-y, yank
  705. InsertText (Clipboard.Contents);
  706. selecting = false;
  707. break;
  708. case Key.ControlSpace:
  709. selecting = true;
  710. selectionStartColumn = currentColumn;
  711. selectionStartRow = currentRow;
  712. break;
  713. case (Key)((int)'w' + Key.AltMask):
  714. SetClipboard (GetRegion ());
  715. selecting = false;
  716. break;
  717. case Key.ControlW:
  718. SetClipboard (GetRegion ());
  719. ClearRegion ();
  720. selecting = false;
  721. break;
  722. case (Key)((int)'b' + Key.AltMask):
  723. break;
  724. case (Key)((int)'f' + Key.AltMask):
  725. break;
  726. case Key.Enter:
  727. var orow = currentRow;
  728. currentLine = GetCurrentLine ();
  729. restCount = currentLine.Count - currentColumn;
  730. rest = currentLine.GetRange (currentColumn, restCount);
  731. currentLine.RemoveRange (currentColumn, restCount);
  732. model.AddLine (currentRow + 1, rest);
  733. currentRow++;
  734. bool fullNeedsDisplay = false;
  735. if (currentRow >= topRow + Frame.Height) {
  736. topRow++;
  737. fullNeedsDisplay = true;
  738. }
  739. currentColumn = 0;
  740. if (currentColumn < leftColumn) {
  741. fullNeedsDisplay = true;
  742. leftColumn = 0;
  743. }
  744. if (fullNeedsDisplay)
  745. SetNeedsDisplay ();
  746. else
  747. SetNeedsDisplay (new Rect (0, currentRow - topRow, 0, Frame.Height));
  748. break;
  749. default:
  750. // Ignore control characters and other special keys
  751. if (kb.Key < Key.Space || kb.Key > Key.CharMask)
  752. return false;
  753. Insert ((uint)kb.Key);
  754. currentColumn++;
  755. if (currentColumn >= leftColumn + Frame.Width) {
  756. leftColumn++;
  757. SetNeedsDisplay ();
  758. }
  759. PositionCursor ();
  760. return true;
  761. }
  762. return true;
  763. }
  764. #if false
  765. int WordForward (int p)
  766. {
  767. if (p >= text.Length)
  768. return -1;
  769. int i = p;
  770. if (Rune.IsPunctuation (text [p]) || Rune.IsWhiteSpace (text [p])) {
  771. for (; i < text.Length; i++) {
  772. var r = text [i];
  773. if (Rune.IsLetterOrDigit (r))
  774. break;
  775. }
  776. for (; i < text.Length; i++) {
  777. var r = text [i];
  778. if (!Rune.IsLetterOrDigit (r))
  779. break;
  780. }
  781. } else {
  782. for (; i < text.Length; i++) {
  783. var r = text [i];
  784. if (!Rune.IsLetterOrDigit (r))
  785. break;
  786. }
  787. }
  788. if (i != p)
  789. return i;
  790. return -1;
  791. }
  792. int WordBackward (int p)
  793. {
  794. if (p == 0)
  795. return -1;
  796. int i = p - 1;
  797. if (i == 0)
  798. return 0;
  799. var ti = text [i];
  800. if (Rune.IsPunctuation (ti) || Rune.IsSymbol (ti) || Rune.IsWhiteSpace (ti)) {
  801. for (; i >= 0; i--) {
  802. if (Rune.IsLetterOrDigit (text [i]))
  803. break;
  804. }
  805. for (; i >= 0; i--) {
  806. if (!Rune.IsLetterOrDigit (text [i]))
  807. break;
  808. }
  809. } else {
  810. for (; i >= 0; i--) {
  811. if (!Rune.IsLetterOrDigit (text [i]))
  812. break;
  813. }
  814. }
  815. i++;
  816. if (i != p)
  817. return i;
  818. return -1;
  819. }
  820. public override bool MouseEvent (MouseEvent ev)
  821. {
  822. if (!ev.Flags.HasFlag (MouseFlags.Button1Clicked))
  823. return false;
  824. if (!HasFocus)
  825. SuperView.SetFocus (this);
  826. // We could also set the cursor position.
  827. point = first + ev.X;
  828. if (point > text.Length)
  829. point = text.Length;
  830. if (point < first)
  831. point = 0;
  832. SetNeedsDisplay ();
  833. return true;
  834. }
  835. #endif
  836. }
  837. }