diff --git a/PSReadLine/BasicEditing.cs b/PSReadLine/BasicEditing.cs index 33405a88..85b77bff 100644 --- a/PSReadLine/BasicEditing.cs +++ b/PSReadLine/BasicEditing.cs @@ -157,10 +157,8 @@ private static void BackwardDeleteSubstring(int position, Action position) { var count = _singleton._current - position; - - _clipboard.Record(_singleton._buffer, position, count); - _singleton.SaveEditItem(EditItemDelete.Create(_clipboard, position, instigator)); - _singleton._buffer.Remove(position, count); + + _singleton.RemoveTextToViRegister(position, count, instigator); _singleton._current = position; _singleton.Render(); } @@ -185,15 +183,8 @@ public static void BackwardDeleteChar(ConsoleKeyInfo? key = null, object arg = n qty = Math.Min(qty, _singleton._current); int startDeleteIndex = _singleton._current - qty; - _singleton.SaveEditItem( - EditItemDelete.Create( - _singleton._buffer.ToString(startDeleteIndex, qty), - startDeleteIndex, - BackwardDeleteChar, - arg) - ); - _singleton.SaveToClipboard(startDeleteIndex, qty); - _singleton._buffer.Remove(startDeleteIndex, qty); + + _singleton.RemoveTextToViRegister(startDeleteIndex, qty, BackwardDeleteChar, arg); _singleton._current = startDeleteIndex; _singleton.Render(); } @@ -214,9 +205,7 @@ private void DeleteCharImpl(int qty, bool orExit) { qty = Math.Min(qty, _singleton._buffer.Length - _singleton._current); - SaveEditItem(EditItemDelete.Create(_buffer.ToString(_current, qty), _current, DeleteChar, qty)); - SaveToClipboard(_current, qty); - _buffer.Remove(_current, qty); + RemoveTextToViRegister(_current, qty, DeleteChar, qty); if (_current >= _buffer.Length) { _current = Math.Max(0, _buffer.Length + ViEndOfLineFactor); diff --git a/PSReadLine/ReadLine.cs b/PSReadLine/ReadLine.cs index 4ace2a84..4395a379 100644 --- a/PSReadLine/ReadLine.cs +++ b/PSReadLine/ReadLine.cs @@ -630,7 +630,7 @@ void ProcessOneKey(PSKeyInfo key, Dictionary dispatchTabl static PSConsoleReadLine() { _singleton = new PSConsoleReadLine(); - _clipboard = new ViRegister(_singleton); + _viRegister = new ViRegister(_singleton); } private PSConsoleReadLine() diff --git a/PSReadLine/ReadLine.vi.cs b/PSReadLine/ReadLine.vi.cs index 52e678a9..9c6417ae 100644 --- a/PSReadLine/ReadLine.vi.cs +++ b/PSReadLine/ReadLine.vi.cs @@ -237,14 +237,11 @@ public static void DeleteToEnd(ConsoleKeyInfo? key = null, object arg = null) var length = endPosition - startPosition + 1; if (length > 0) { - _clipboard.Record(_singleton._buffer, startPosition, length); - _singleton.SaveEditItem(EditItemDelete.Create( - _clipboard, - _singleton._current, + _singleton.RemoveTextToViRegister( + startPosition, + length, DeleteToEnd, - arg)); - - _singleton._buffer.Remove(_singleton._current, length); + arg); // the cursor will go back one character, unless at the beginning of the line var endOfLineCursorPos = GetEndOfLogicalLinePos(_singleton._current) - 1; @@ -282,14 +279,12 @@ public static void DeleteWord(ConsoleKeyInfo? key = null, object arg = null) private static void DeleteToEndPoint(object arg, int endPoint, Action instigator) { - _singleton.SaveToClipboard(_singleton._current, endPoint - _singleton._current); - _singleton.SaveEditItem(EditItemDelete.Create( - _clipboard, + _singleton.RemoveTextToViRegister( _singleton._current, + endPoint - _singleton._current, instigator, - arg - )); - _singleton._buffer.Remove(_singleton._current, endPoint - _singleton._current); + arg); + if (_singleton._current >= _singleton._buffer.Length) { _singleton._current = Math.Max(0, _singleton._buffer.Length - 1); @@ -301,14 +296,12 @@ private static void DeleteBackwardToEndPoint(object arg, int endPoint, Action= _singleton._buffer.Length) - { - _singleton._current = Math.Max(0, _singleton._buffer.Length - 1); - } - _singleton.Render(); + DeleteToEndPoint(arg, endPoint, ViDeleteGlob); } /// @@ -358,19 +338,8 @@ public static void DeleteEndOfWord(ConsoleKeyInfo? key = null, object arg = null Ding(); return; } - _singleton.SaveToClipboard(_singleton._current, 1 + endPoint - _singleton._current); - _singleton.SaveEditItem(EditItemDelete.Create( - _clipboard, - _singleton._current, - DeleteEndOfWord, - arg - )); - _singleton._buffer.Remove(_singleton._current, 1 + endPoint - _singleton._current); - if (_singleton._current >= _singleton._buffer.Length) - { - _singleton._current = Math.Max(0, _singleton._buffer.Length - 1); - } - _singleton.Render(); + + DeleteToEndPoint(arg, 1 + endPoint, DeleteEndOfWord); } /// @@ -385,19 +354,7 @@ public static void ViDeleteEndOfGlob(ConsoleKeyInfo? key = null, object arg = nu endPoint = _singleton.ViFindGlobEnd(endPoint); } - _singleton.SaveToClipboard(_singleton._current, 1 + endPoint - _singleton._current); - _singleton.SaveEditItem(EditItemDelete.Create( - _clipboard, - _singleton._current, - ViDeleteEndOfGlob, - arg - )); - _singleton._buffer.Remove(_singleton._current, 1 + endPoint - _singleton._current); - if (_singleton._current >= _singleton._buffer.Length) - { - _singleton._current = Math.Max(0, _singleton._buffer.Length - 1); - } - _singleton.Render(); + DeleteToEndPoint(arg, 1 + endPoint, ViDeleteEndOfGlob); } /// @@ -724,10 +681,12 @@ public static void DeleteLineToFirstChar(ConsoleKeyInfo? key = null, object arg { var i = GetFirstNonBlankOfLogicalLinePos(_singleton._current); - _singleton.SaveToClipboard(i, _singleton._current - i); - _singleton.SaveEditItem(EditItemDelete.Create(_clipboard, i, DeleteLineToFirstChar)); + _singleton.RemoveTextToViRegister( + i, + _singleton._current - i, + DeleteLineToFirstChar, + arg); - _singleton._buffer.Remove(i, _singleton._current - i); _singleton._current = i; _singleton.Render(); } @@ -775,7 +734,7 @@ private static int DeleteLineImpl(int lineIndex, int lineCount) var deleteText = _singleton._buffer.ToString(range.Offset, range.Count); - _clipboard.LinewiseRecord(deleteText); + _viRegister.LinewiseRecord(deleteText); var deletePosition = range.Offset; var anchor = _singleton._current; @@ -890,14 +849,12 @@ public static void BackwardDeleteWord(ConsoleKeyInfo? key = null, object arg = n Ding(); return; } - _clipboard.Record(_singleton._buffer, deletePoint, _singleton._current - deletePoint); - _singleton.SaveEditItem(EditItemDelete.Create( - _clipboard, + _singleton.RemoveTextToViRegister( deletePoint, + _singleton._current - deletePoint, BackwardDeleteWord, - arg - )); - _singleton._buffer.Remove(deletePoint, _singleton._current - deletePoint); + arg); + _singleton._current = deletePoint; _singleton.Render(); } @@ -923,14 +880,12 @@ public static void ViBackwardDeleteGlob(ConsoleKeyInfo? key = null, object arg = Ding(); return; } - _clipboard.Record(_singleton._buffer, deletePoint, _singleton._current - deletePoint); - _singleton.SaveEditItem(EditItemDelete.Create( - _clipboard, + _singleton.RemoveTextToViRegister( deletePoint, + _singleton._current - deletePoint, BackwardDeleteWord, - arg - )); - _singleton._buffer.Remove(deletePoint, _singleton._current - deletePoint); + arg); + _singleton._current = deletePoint; _singleton.Render(); } @@ -966,10 +921,12 @@ private static void DeleteRange(int first, int last, Action /// Paste the clipboard after the cursor, moving the cursor to the end of the pasted text. /// public static void PasteAfter(ConsoleKeyInfo? key = null, object arg = null) { - if (_clipboard.IsEmpty) + if (_viRegister.IsEmpty) { Ding(); return; @@ -33,7 +33,7 @@ public static void PasteAfter(ConsoleKeyInfo? key = null, object arg = null) /// public static void PasteBefore(ConsoleKeyInfo? key = null, object arg = null) { - if (_clipboard.IsEmpty) + if (_viRegister.IsEmpty) { Ding(); return; @@ -43,19 +43,19 @@ public static void PasteBefore(ConsoleKeyInfo? key = null, object arg = null) private void PasteAfterImpl() { - _current = _clipboard.PasteAfter(_buffer, _current); + _current = _viRegister.PasteAfter(_buffer, _current); Render(); } private void PasteBeforeImpl() { - _current = _clipboard.PasteBefore(_buffer, _current); + _current = _viRegister.PasteBefore(_buffer, _current); Render(); } private void SaveToClipboard(int startIndex, int length) { - _clipboard.Record(_buffer, startIndex, length); + _viRegister.Record(_buffer, startIndex, length); } /// @@ -67,7 +67,26 @@ private void SaveToClipboard(int startIndex, int length) private void SaveLinesToClipboard(int lineIndex, int lineCount) { var range = _buffer.GetRange(lineIndex, lineCount); - _clipboard.LinewiseRecord(_buffer.ToString(range.Offset, range.Count)); + _viRegister.LinewiseRecord(_buffer.ToString(range.Offset, range.Count)); + } + + /// + /// Remove a portion of text from the buffer, save it to the vi register + /// and also save it to the edit list to support undo. + /// + /// + /// + /// + /// + private void RemoveTextToViRegister(int start, int count, Action instigator = null, object arg = null) + { + _singleton.SaveToClipboard(start, count); + _singleton.SaveEditItem(EditItemDelete.Create( + _viRegister.RawText, + start, + instigator, + arg)); + _singleton._buffer.Remove(start, count); } /// @@ -142,7 +161,7 @@ public static void ViYankToEndOfLine(ConsoleKeyInfo? key = null, object arg = nu var length = end - start + 1; if (length > 0) { - _clipboard.Record(_singleton._buffer, start, length); + _singleton.SaveToClipboard(start, length); } } @@ -254,8 +273,8 @@ public static void ViYankBeginningOfLine(ConsoleKeyInfo? key = null, object arg var start = GetBeginningOfLinePos(_singleton._current); var length = _singleton._current - start; if (length > 0) - { - _clipboard.Record(_singleton._buffer, start, length); + { + _singleton.SaveToClipboard(start, length); _singleton.MoveCursor(start); } } @@ -269,7 +288,7 @@ public static void ViYankToFirstChar(ConsoleKeyInfo? key = null, object arg = nu var length = _singleton._current - start; if (length > 0) { - _clipboard.Record(_singleton._buffer, start, length); + _singleton.SaveToClipboard(start, length); _singleton.MoveCursor(start); } }