TextView.cs 24 KB

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