static void NextWord (TextEditorData data, bool subword) { int oldLine = data.Caret.Line; int offset = subword? data.FindNextSubwordOffset (data.Caret.Offset) : data.FindNextWordOffset (data.Caret.Offset); if (data.Caret.Offset != offset && data.CanEdit (oldLine) && data.CanEdit (data.Caret.Line)) { data.Remove (data.Caret.Offset, offset - data.Caret.Offset); data.Document.CommitLineToEndUpdate (data.Caret.Line); } }
static void NextWord (TextEditorData data, bool subword) { int oldLine = data.Caret.Line; int caretOffset = data.Caret.Offset; int offset = subword? data.FindNextSubwordOffset (caretOffset) : data.FindNextWordOffset (caretOffset); if (caretOffset != offset && data.CanEdit (oldLine) && data.CanEdit (data.Caret.Line)) { data.Remove (caretOffset, offset - caretOffset); } }
static void PreviousWord (TextEditorData data, bool subword) { int oldLine = data.Caret.Line; int offset = subword? data.FindPrevSubwordOffset (data.Caret.Offset) : data.FindPrevWordOffset (data.Caret.Offset); if (data.Caret.Offset != offset && data.CanEdit (oldLine) && data.CanEdit (data.Caret.Line)) { data.Remove (offset, data.Caret.Offset - offset); data.Caret.Offset = offset; if (oldLine != data.Caret.Line) data.Document.CommitLineToEndUpdate (data.Caret.Line); } }
static void NextWord(TextEditorData data, bool subword) { int oldLine = data.Caret.Line; int caretOffset = data.Caret.Offset; int offset = subword? data.FindNextSubwordOffset(caretOffset) : data.FindNextWordOffset(caretOffset); if (caretOffset != offset && data.CanEdit(oldLine) && data.CanEdit(data.Caret.Line)) { data.Remove(caretOffset, offset - caretOffset); } }
static void NextWord(TextEditorData data, bool subword) { int oldLine = data.Caret.Line; int offset = subword? data.FindNextSubwordOffset(data.Caret.Offset) : data.FindNextWordOffset(data.Caret.Offset); if (data.Caret.Offset != offset && data.CanEdit(oldLine) && data.CanEdit(data.Caret.Line)) { data.Remove(data.Caret.Offset, offset - data.Caret.Offset); data.Document.CommitLineToEndUpdate(data.Caret.Line); } }
static void PreviousWord (TextEditorData data, bool subword) { using (var undo = data.OpenUndoGroup ()) { int oldLine = data.Caret.Line; int caretOffset = data.Caret.Offset; int offset = subword ? data.FindPrevSubwordOffset (caretOffset) : data.FindPrevWordOffset (caretOffset); if (caretOffset != offset && data.CanEdit (oldLine) && data.CanEdit (data.Caret.Line)) { data.Remove (offset, caretOffset - offset); } } }
static void PreviousWord(TextEditorData data, bool subword) { using (var undo = data.OpenUndoGroup()) { int oldLine = data.Caret.Line; int caretOffset = data.Caret.Offset; int offset = subword ? data.FindPrevSubwordOffset(caretOffset) : data.FindPrevWordOffset(caretOffset); if (caretOffset != offset && data.CanEdit(oldLine) && data.CanEdit(data.Caret.Line)) { data.Remove(offset, caretOffset - offset); } } }
static void PreviousWord(TextEditorData data, bool subword) { int oldLine = data.Caret.Line; int offset = subword? data.FindPrevSubwordOffset(data.Caret.Offset) : data.FindPrevWordOffset(data.Caret.Offset); if (data.Caret.Offset != offset && data.CanEdit(oldLine) && data.CanEdit(data.Caret.Line)) { data.Remove(offset, data.Caret.Offset - offset); data.Caret.Offset = offset; if (oldLine != data.Caret.Line) { data.Document.CommitLineToEndUpdate(data.Caret.Line); } } }
public static void CaretLineToEnd(TextEditorData data) { if (!data.CanEdit(data.Caret.Line)) { return; } var line = data.Document.GetLine(data.Caret.Line); using (var undo = data.OpenUndoGroup()) { data.EnsureCaretIsNotVirtual(); int physColumn = data.Caret.Column - 1; if (physColumn == line.Length) { // Nothing after the cursor, delete the end-of-line sequence data.Remove(line.Offset + physColumn, line.LengthIncludingDelimiter - physColumn); } else { // Delete from cursor position to the end of the line var end = GetEndOfLineOffset(data, data.Caret.Location, false); data.Remove(line.Offset + physColumn, end - (line.Offset + physColumn)); } } data.Document.CommitLineUpdate(data.Caret.Line); }
public static void CaretLine(TextEditorData data) { if (data.Document.LineCount <= 1 || !data.CanEdit(data.Caret.Line)) { return; } LineSegment line = data.Document.GetLine(data.Caret.Line); data.Remove(line.Offset, line.Length); data.Document.CommitLineToEndUpdate(data.Caret.Line); data.Caret.CheckCaretPosition(); }
public static void CaretLine(TextEditorData data) { if (data.Document.LineCount <= 1 || !data.CanEdit(data.Caret.Line)) { return; } var start = GetStartOfLineOffset(data, data.Caret.Location); var end = GetEndOfLineOffset(data, data.Caret.Location); data.Remove(start, end - start); data.Caret.Column = DocumentLocation.MinColumn; }
public static void CaretLineToStart(TextEditorData data) { if (!data.CanEdit(data.Caret.Line)) { return; } var line = data.Document.GetLine(data.Caret.Line); using (var undo = data.OpenUndoGroup()) { data.EnsureCaretIsNotVirtual(); int physColumn = data.Caret.Column - 1; var startLine = GetStartOfLineOffset(data, data.Caret.Location); data.Remove(startLine, (line.Offset + physColumn) - startLine); } data.Document.CommitLineUpdate(data.Caret.Line); }
public static void CaretLineToEnd(TextEditorData data) { if (!data.CanEdit(data.Caret.Line)) { return; } LineSegment line = data.Document.GetLine(data.Caret.Line); if (data.Caret.Column == line.EditableLength) { // Nothing after the cursor, delete the end-of-line sequence data.Remove(line.Offset + data.Caret.Column, line.Length - data.Caret.Column); } else { // Delete from cursor position to the end of the line data.Remove(line.Offset + data.Caret.Column, line.EditableLength - data.Caret.Column); } data.Document.CommitLineUpdate(data.Caret.Line); }
public static void CaretLine(TextEditorData data) { if (data.Document.LineCount <= 1 || !data.CanEdit(data.Caret.Line)) { return; } using (var undo = data.OpenUndoGroup()) { if (data.IsSomethingSelected) { var startLine = data.GetLine(data.MainSelection.Start.Line); var endLine = data.GetLine(data.MainSelection.End.Line); data.Remove(startLine.Offset, endLine.EndOffsetIncludingDelimiter - startLine.Offset); return; } var start = GetStartOfLineOffset(data, data.Caret.Location); var end = GetEndOfLineOffset(data, data.Caret.Location); data.Remove(start, end - start); data.Caret.Column = DocumentLocation.MinColumn; } }
static int PasteFrom(Clipboard clipboard, TextEditorData data, bool preserveSelection, int insertionOffset, bool preserveState) { int result = -1; if (!data.CanEdit(data.Document.OffsetToLineNumber(insertionOffset))) { return(result); } if (clipboard.WaitIsTargetAvailable(CopyOperation.MD_ATOM)) { clipboard.RequestContents(CopyOperation.MD_ATOM, delegate(Clipboard clp, SelectionData selectionData) { if (selectionData.Length > 0) { byte[] selBytes = selectionData.Data; string text = System.Text.Encoding.UTF8.GetString(selBytes, 1, selBytes.Length - 1); bool pasteBlock = (selBytes [0] & 1) == 1; bool pasteLine = (selBytes [0] & 2) == 2; var clearSelection = data.IsSomethingSelected ? data.MainSelection.SelectionMode != SelectionMode.Block : true; using (var undo = data.OpenUndoGroup()) { if (pasteBlock) { data.Caret.PreserveSelection = true; if (preserveSelection && data.IsSomethingSelected) { data.DeleteSelectedText(clearSelection); } string[] lines = text.Split('\r'); int lineNr = data.Document.OffsetToLineNumber(insertionOffset); int col = insertionOffset - data.Document.GetLine(lineNr).Offset; int visCol = data.Document.GetLine(lineNr).GetVisualColumn(data, col); DocumentLine curLine; int lineCol = col; result = 0; for (int i = 0; i < lines.Length; i++) { while (data.Document.LineCount <= lineNr + i) { data.Insert(data.Document.TextLength, Environment.NewLine); result += Environment.NewLine.Length; } curLine = data.Document.GetLine(lineNr + i); if (lines [i].Length > 0) { lineCol = curLine.GetLogicalColumn(data, visCol); if (curLine.Length + 1 < lineCol) { result += lineCol - curLine.Length; data.Insert(curLine.Offset + curLine.Length, new string (' ', lineCol - curLine.Length)); } data.Insert(curLine.Offset + lineCol, lines [i]); result += lines [i].Length; } if (!preserveState) { data.Caret.Offset = curLine.Offset + lineCol + lines [i].Length; } } if (!preserveState) { data.ClearSelection(); } data.Caret.PreserveSelection = false; } else if (pasteLine) { data.Caret.PreserveSelection = true; if (preserveSelection && data.IsSomethingSelected) { data.DeleteSelectedText(clearSelection); } result = text.Length; DocumentLine curLine = data.Document.GetLine(data.Caret.Line); data.Insert(curLine.Offset, text + data.EolMarker); if (!preserveState) { data.ClearSelection(); } data.Caret.PreserveSelection = false; } else { PastePlainText(data, text); } } } }); // we got MD_ATOM text - no need to request text. (otherwise buffer may get copied twice). return(result); } if (result < 0 && clipboard.WaitIsTextAvailable()) { clipboard.RequestText(delegate(Clipboard clp, string text) { if (string.IsNullOrEmpty(text)) { return; } using (var undo = data.OpenUndoGroup()) { PastePlainText(data, text); } }); } return(result); }
static int PasteFrom (Clipboard clipboard, TextEditorData data, bool preserveSelection, int insertionOffset, bool preserveState) { int result = -1; if (!data.CanEdit (data.Document.OffsetToLineNumber (insertionOffset))) return result; clipboard.RequestContents (CopyOperation.MD_ATOM, delegate(Clipboard clp, SelectionData selectionData) { if (selectionData.Length > 0) { byte[] selBytes = selectionData.Data; string text = System.Text.Encoding.UTF8.GetString (selBytes, 1, selBytes.Length - 1); bool pasteBlock = (selBytes[0] & 1) == 1; bool pasteLine = (selBytes[0] & 2) == 2; if (!pasteBlock && !pasteLine) return; data.Document.BeginAtomicUndo (); if (preserveSelection && data.IsSomethingSelected) data.DeleteSelectedText (); data.Caret.PreserveSelection = true; if (pasteBlock) { string[] lines = text.Split ('\r'); int lineNr = data.Document.OffsetToLineNumber (insertionOffset); int col = insertionOffset - data.Document.GetLine (lineNr).Offset; int visCol = data.Document.GetLine (lineNr).GetVisualColumn (data, col); LineSegment curLine; int lineCol = col; result = 0; for (int i = 0; i < lines.Length; i++) { while (data.Document.LineCount <= lineNr + i) { data.Insert (data.Document.Length, Environment.NewLine); result += Environment.NewLine.Length; } curLine = data.Document.GetLine (lineNr + i); if (lines[i].Length > 0) { lineCol = curLine.GetLogicalColumn (data, visCol); if (curLine.EditableLength + 1 < lineCol) { result += lineCol - curLine.EditableLength; data.Insert (curLine.Offset + curLine.EditableLength, new string (' ', lineCol - curLine.EditableLength)); } data.Insert (curLine.Offset + lineCol, lines[i]); result += lines[i].Length; } if (!preserveState) data.Caret.Offset = curLine.Offset + lineCol + lines[i].Length; } } else if (pasteLine) { result += text.Length; LineSegment curLine = data.Document.GetLine (data.Caret.Line); data.Insert (curLine.Offset, text + data.EolMarker); if (!preserveState) data.Caret.Offset += text.Length + data.EolMarker.Length; } /* data.MainSelection = new Selection (data.Document.OffsetToLocation (insertionOffset), data.Caret.Location, lines.Length > 1 ? SelectionMode.Block : SelectionMode.Normal);*/ if (!preserveState) data.ClearSelection (); data.Caret.PreserveSelection = false; data.Document.EndAtomicUndo (); } }); if (result < 0) { clipboard.WaitIsTextAvailable (); clipboard.RequestText (delegate(Clipboard clp, string text) { if (string.IsNullOrEmpty (text)) return; data.Document.BeginAtomicUndo (); int caretPos = data.Caret.Offset; if (data.IsSomethingSelected && data.MainSelection.SelectionMode == SelectionMode.Block) { data.Caret.PreserveSelection = true; if (!data.MainSelection.IsDirty) { data.DeleteSelectedText (false); data.MainSelection.IsDirty = true; } int textLength = 0; int column = data.Caret.Column; int minLine = data.MainSelection.MinLine; int maxLine = data.MainSelection.MaxLine; for (int lineNumber = minLine; lineNumber <= maxLine; lineNumber++) { int offset = data.Document.GetLine (lineNumber).Offset + column; textLength = data.Insert (offset, text); data.PasteText (offset, text); } data.Caret.Offset += textLength; data.MainSelection.Anchor = new DocumentLocation (data.Caret.Line == minLine ? maxLine : minLine, data.Caret.Column - textLength); data.MainSelection.Lead = new DocumentLocation (data.Caret.Line, data.Caret.Column); data.Caret.PreserveSelection = false; data.Document.CommitMultipleLineUpdate (data.MainSelection.MinLine, data.MainSelection.MaxLine); } else { ISegment selection = data.SelectionRange; if (preserveSelection && data.IsSomethingSelected) data.DeleteSelectedText (); data.Caret.PreserveSelection = true; //int oldLine = data.Caret.Line; int textLength = data.Insert (insertionOffset, text); result = textLength; if (data.IsSomethingSelected && data.SelectionRange.Offset >= insertionOffset) data.SelectionRange.Offset += textLength; if (data.IsSomethingSelected && data.MainSelection.GetAnchorOffset (data) >= insertionOffset) data.MainSelection.Anchor = data.Document.OffsetToLocation (data.MainSelection.GetAnchorOffset (data) + textLength); data.Caret.PreserveSelection = false; if (!preserveState) { data.Caret.Offset += textLength; } else { if (caretPos >= insertionOffset) data.Caret.Offset += textLength; if (selection != null) { int offset = selection.Offset; if (offset >= insertionOffset) offset += textLength; data.SelectionRange = new Segment (offset, selection.Length); } } data.PasteText (insertionOffset, text); } data.Document.EndAtomicUndo (); }); } return result; }
static int PasteFrom(Clipboard clipboard, TextEditorData data, bool preserveSelection, int insertionOffset, bool preserveState) { int result = -1; if (!data.CanEdit(data.Document.OffsetToLineNumber(insertionOffset))) { return(result); } if (clipboard.WaitIsTargetAvailable(CopyOperation.MD_ATOM)) { clipboard.RequestContents(CopyOperation.MD_ATOM, (ClipboardReceivedFunc) delegate(Clipboard clp, SelectionData selectionData) { if (selectionData.Length > 0) { byte[] selBytes = selectionData.Data; var upperBound = System.Math.Max(0, System.Math.Min(selBytes [1], selBytes.Length - 2)); byte[] copyData = new byte[upperBound]; Array.Copy(selBytes, 2, copyData, 0, copyData.Length); var rawTextOffset = 1 + 1 + copyData.Length; string text = Encoding.UTF8.GetString(selBytes, rawTextOffset, selBytes.Length - rawTextOffset); bool pasteBlock = (selBytes [0] & 1) == 1; bool pasteLine = (selBytes [0] & 2) == 2; if (pasteBlock) { using (var undo = data.OpenUndoGroup()) { var version = data.Document.Version; if (!preserveSelection) { data.DeleteSelectedText(!data.IsSomethingSelected || data.MainSelection.SelectionMode != MonoDevelop.Ide.Editor.SelectionMode.Block); } int startLine = data.Caret.Line; data.EnsureCaretIsNotVirtual(); insertionOffset = version.MoveOffsetTo(data.Document.Version, insertionOffset); data.Caret.PreserveSelection = true; var lines = new List <string> (); int offset = 0; while (true) { var delimiter = LineSplitter.NextDelimiter(text, offset); if (delimiter.IsInvalid) { break; } int delimiterEndOffset = delimiter.EndOffset; lines.Add(text.Substring(offset, delimiter.Offset - offset)); offset = delimiterEndOffset; } if (offset < text.Length) { lines.Add(text.Substring(offset, text.Length - offset)); } int lineNr = data.Document.OffsetToLineNumber(insertionOffset); int col = insertionOffset - data.Document.GetLine(lineNr).Offset; int visCol = data.Document.GetLine(lineNr).GetVisualColumn(data, col); DocumentLine curLine; int lineCol = col; result = 0; for (int i = 0; i < lines.Count; i++) { while (data.Document.LineCount <= lineNr + i) { data.Insert((int)data.Document.Length, Environment.NewLine); result += Environment.NewLine.Length; } curLine = data.Document.GetLine(lineNr + i); if (lines [i].Length > 0) { lineCol = curLine.GetLogicalColumn(data, visCol); if (curLine.Length + 1 < lineCol) { result += lineCol - curLine.Length; data.Insert(curLine.Offset + curLine.Length, new string (' ', lineCol - curLine.Length)); } data.Insert(curLine.Offset + lineCol, lines [i]); result += lines [i].Length; } if (!preserveState) { data.Caret.Offset = curLine.Offset + lineCol + lines [i].Length; } } if (!preserveState) { data.ClearSelection(); } data.FixVirtualIndentation(startLine); data.Caret.PreserveSelection = false; } } else if (pasteLine) { using (var undo = data.OpenUndoGroup()) { if (!preserveSelection) { data.DeleteSelectedText(!data.IsSomethingSelected || data.MainSelection.SelectionMode != MonoDevelop.Ide.Editor.SelectionMode.Block); } data.EnsureCaretIsNotVirtual(); data.Caret.PreserveSelection = true; result = text.Length; DocumentLine curLine = data.Document.GetLine(data.Caret.Line); result = PastePlainText(data, curLine.Offset, text + data.EolMarker, preserveSelection, copyData); if (!preserveState) { data.ClearSelection(); } data.Caret.PreserveSelection = false; data.FixVirtualIndentation(curLine.LineNumber); } } else { result = PastePlainText(data, insertionOffset, text, preserveSelection, copyData); } } }); // we got MD_ATOM text - no need to request text. (otherwise buffer may get copied twice). return(result); } if (result < 0 && clipboard.WaitIsTextAvailable()) { clipboard.RequestText(delegate(Clipboard clp, string text) { if (string.IsNullOrEmpty(text)) { return; } result = PastePlainText(data, insertionOffset, text, preserveSelection); }); } return(result); }
static int PasteFrom(TextEditorData data, bool preserveSelection, int insertionOffset, bool preserveState) { int result = -1; if (!data.CanEdit(data.Document.OffsetToLineNumber(insertionOffset))) { return(result); } /*var selectionData = Clipboard.GetData<SelectionData> (); * Clipboard.BeginGetText( delegate(Clipboard clp, SelectionData selectionData) { * if (selectionData.Length > 0) { * byte[] selBytes = selectionData.Data; * byte[] copyData = new byte[selBytes[1]]; * Array.Copy (selBytes, 2, copyData, 0, copyData.Length); * var rawTextOffset = 1 + 1 + copyData.Length; * string text = System.Text.Encoding.UTF8.GetString (selBytes, rawTextOffset, selBytes.Length - rawTextOffset); * bool pasteBlock = (selBytes [0] & 1) == 1; * bool pasteLine = (selBytes [0] & 2) == 2; * if (pasteBlock) { * using (var undo = data.OpenUndoGroup ()) { * var version = data.Document.Version; * if (!preserveSelection) * data.DeleteSelectedText (!data.IsSomethingSelected || data.MainSelection.SelectionMode != SelectionMode.Block); * data.EnsureCaretIsNotVirtual (); * insertionOffset = version.MoveOffsetTo (data.Document.Version, insertionOffset); * * data.Caret.PreserveSelection = true; * var lines = new List<string> (); * int offset = 0; * while (true) { * var delimiter = LineSplitter.NextDelimiter (text, offset); * if (delimiter.IsInvalid) * break; * * int delimiterEndOffset = delimiter.Offset + delimiter.Length; * lines.Add (text.Substring (offset, delimiter.Offset - offset)); * offset = delimiterEndOffset; * } * if (offset < text.Length) * lines.Add (text.Substring (offset, text.Length - offset)); * * int lineNr = data.Document.OffsetToLineNumber (insertionOffset); * int col = insertionOffset - data.Document.GetLine (lineNr).Offset; * int visCol = data.Document.GetLine (lineNr).GetVisualColumn (data, col); * DocumentLine curLine; * int lineCol = col; * result = 0; * for (int i = 0; i < lines.Count; i++) { * while (data.Document.LineCount <= lineNr + i) { * data.Insert (data.Document.TextLength, Environment.NewLine); * result += Environment.NewLine.Length; * } * curLine = data.Document.GetLine (lineNr + i); * if (lines [i].Length > 0) { * lineCol = curLine.GetLogicalColumn (data, visCol); * if (curLine.Length + 1 < lineCol) { * result += lineCol - curLine.Length; * data.Insert (curLine.Offset + curLine.Length, new string (' ', lineCol - curLine.Length)); * } * data.Insert (curLine.Offset + lineCol, lines [i]); * result += lines [i].Length; * } * if (!preserveState) * data.Caret.Offset = curLine.Offset + lineCol + lines [i].Length; * } * if (!preserveState) * data.ClearSelection (); * data.Caret.PreserveSelection = false; * } * } else if (pasteLine) { * using (var undo = data.OpenUndoGroup ()) { * if (!preserveSelection) * data.DeleteSelectedText (!data.IsSomethingSelected || data.MainSelection.SelectionMode != SelectionMode.Block); * data.EnsureCaretIsNotVirtual (); * * data.Caret.PreserveSelection = true; * result = text.Length; * DocumentLine curLine = data.Document.GetLine (data.Caret.Line); * * result = PastePlainText (data, curLine.Offset, text + data.EolMarker, preserveSelection, copyData); * if (!preserveState) * data.ClearSelection (); * data.Caret.PreserveSelection = false; * } * } else { * result = PastePlainText (data, insertionOffset, text, preserveSelection, copyData); * } * } * }); * // we got MD_ATOM text - no need to request text. (otherwise buffer may get copied twice). * return result; * } */ if (result < 0 && Clipboard.ContainsText()) { //TODO: Shall this action be done asynchronously? var text = Clipboard.GetText(); if (string.IsNullOrEmpty(text)) { return(-1); } result = PastePlainText(data, insertionOffset, text, preserveSelection); } return(result); }
static int PasteFrom(Clipboard clipboard, TextEditorData data, bool preserveSelection, int insertionOffset, bool preserveState) { int result = -1; if (!data.CanEdit(data.Document.OffsetToLineNumber(insertionOffset))) { return(result); } if (clipboard.WaitIsTargetAvailable(CopyOperation.MD_ATOM)) { clipboard.RequestContents(CopyOperation.MD_ATOM, delegate(Clipboard clp, SelectionData selectionData) { if (selectionData.Length > 0) { byte[] selBytes = selectionData.Data; string text = System.Text.Encoding.UTF8.GetString(selBytes, 1, selBytes.Length - 1); bool pasteBlock = (selBytes [0] & 1) == 1; bool pasteLine = (selBytes [0] & 2) == 2; if (!pasteBlock && !pasteLine) { return; } data.Document.BeginAtomicUndo(); if (preserveSelection && data.IsSomethingSelected) { data.DeleteSelectedText(); } data.Caret.PreserveSelection = true; if (pasteBlock) { string[] lines = text.Split('\r'); int lineNr = data.Document.OffsetToLineNumber(insertionOffset); int col = insertionOffset - data.Document.GetLine(lineNr).Offset; int visCol = data.Document.GetLine(lineNr).GetVisualColumn(data, col); LineSegment curLine; int lineCol = col; result = 0; for (int i = 0; i < lines.Length; i++) { while (data.Document.LineCount <= lineNr + i) { data.Insert(data.Document.Length, Environment.NewLine); result += Environment.NewLine.Length; } curLine = data.Document.GetLine(lineNr + i); if (lines [i].Length > 0) { lineCol = curLine.GetLogicalColumn(data, visCol); if (curLine.EditableLength + 1 < lineCol) { result += lineCol - curLine.EditableLength; data.Insert(curLine.Offset + curLine.EditableLength, new string (' ', lineCol - curLine.EditableLength)); } data.Insert(curLine.Offset + lineCol, lines [i]); result += lines [i].Length; } if (!preserveState) { data.Caret.Offset = curLine.Offset + lineCol + lines [i].Length; } } } else if (pasteLine) { result = text.Length; LineSegment curLine = data.Document.GetLine(data.Caret.Line); data.Insert(curLine.Offset, text + data.EolMarker); if (!preserveState) { data.Caret.Offset += text.Length + data.EolMarker.Length; } } /* data.MainSelection = new Selection (data.Document.OffsetToLocation (insertionOffset), * data.Caret.Location, * lines.Length > 1 ? SelectionMode.Block : SelectionMode.Normal);*/ if (!preserveState) { data.ClearSelection(); } data.Caret.PreserveSelection = false; data.Document.EndAtomicUndo(); } }); } if (result < 0 && clipboard.WaitIsTextAvailable()) { clipboard.RequestText(delegate(Clipboard clp, string text) { if (string.IsNullOrEmpty(text)) { return; } data.Document.BeginAtomicUndo(); int caretPos = data.Caret.Offset; if (data.IsSomethingSelected && data.MainSelection.SelectionMode == SelectionMode.Block) { data.Caret.PreserveSelection = true; data.DeleteSelectedText(false); int textLength = 0; int minLine = data.MainSelection.MinLine; int maxLine = data.MainSelection.MaxLine; var visualInsertLocation = data.LogicalToVisualLocation(data.Caret.Location); for (int lineNumber = minLine; lineNumber <= maxLine; lineNumber++) { LineSegment lineSegment = data.GetLine(lineNumber); int insertOffset = lineSegment.GetLogicalColumn(data, visualInsertLocation.Column) - 1; if (lineSegment.EditableLength < insertOffset) { int visualLastColumn = lineSegment.GetVisualColumn(data, lineSegment.EditableLength + 1); int charsToInsert = visualInsertLocation.Column - visualLastColumn; int spaceCount = charsToInsert % data.Options.TabSize; string textToInsert = new string ('\t', (charsToInsert - spaceCount) / data.Options.TabSize) + new string (' ', spaceCount) + text; insertOffset = lineSegment.EditableLength; data.Insert(lineSegment.Offset + insertOffset, textToInsert); data.PasteText(lineSegment.Offset + insertOffset, textToInsert); } else { textLength = data.Insert(lineSegment.Offset + insertOffset, text); data.PasteText(lineSegment.Offset + insertOffset, text); } } data.Caret.Offset += textLength; data.MainSelection.Anchor = new DocumentLocation(System.Math.Max(DocumentLocation.MinLine, data.Caret.Line == minLine ? maxLine : minLine), System.Math.Max(DocumentLocation.MinColumn, data.Caret.Column - textLength)); data.MainSelection.Lead = new DocumentLocation(data.Caret.Line, data.Caret.Column); data.Caret.PreserveSelection = false; data.Document.CommitMultipleLineUpdate(data.MainSelection.MinLine, data.MainSelection.MaxLine); } else { ISegment selection = data.SelectionRange; if (preserveSelection && data.IsSomethingSelected) { data.DeleteSelectedText(); } data.Caret.PreserveSelection = true; //int oldLine = data.Caret.Line; int textLength = data.Insert(insertionOffset, text); result = textLength; if (data.IsSomethingSelected && data.SelectionRange.Offset >= insertionOffset) { data.SelectionRange.Offset += textLength; } if (data.IsSomethingSelected && data.MainSelection.GetAnchorOffset(data) >= insertionOffset) { data.MainSelection.Anchor = data.Document.OffsetToLocation(data.MainSelection.GetAnchorOffset(data) + textLength); } data.Caret.PreserveSelection = false; if (!preserveState) { data.Caret.Offset += textLength; } else { if (caretPos >= insertionOffset) { data.Caret.Offset += textLength; } if (selection != null) { int offset = selection.Offset; if (offset >= insertionOffset) { offset += textLength; } data.SelectionRange = new Segment(offset, selection.Length); } } data.PasteText(insertionOffset, text); } data.Document.EndAtomicUndo(); }); } return(result); }
public static void CaretLine (TextEditorData data) { if (data.Document.LineCount <= 1 || !data.CanEdit (data.Caret.Line)) return; LineSegment line = data.Document.GetLine (data.Caret.Line); data.Remove (line.Offset, line.Length); data.Document.CommitLineToEndUpdate (data.Caret.Line); data.Caret.CheckCaretPosition (); }
public static void CaretLineToEnd (TextEditorData data) { if (!data.CanEdit (data.Caret.Line)) return; var line = data.Document.GetLine (data.Caret.Line); using (var undo = data.OpenUndoGroup ()) { data.EnsureCaretIsNotVirtual (); int physColumn = data.Caret.Column - 1; if (physColumn == line.Length) { // Nothing after the cursor, delete the end-of-line sequence data.Remove (line.Offset + physColumn, line.LengthIncludingDelimiter - physColumn); } else { // Delete from cursor position to the end of the line var end = GetEndOfLineOffset (data, data.Caret.Location, false); data.Remove (line.Offset + physColumn, end - (line.Offset + physColumn)); } } data.Document.CommitLineUpdate (data.Caret.Line); }
public static void CaretLine (TextEditorData data) { if (data.Document.LineCount <= 1 || !data.CanEdit (data.Caret.Line)) return; var start = GetStartOfLineOffset (data, data.Caret.Location); var end = GetEndOfLineOffset (data, data.Caret.Location); data.Remove (start, end - start); data.Caret.Column = DocumentLocation.MinColumn; }
public static void CaretLineToStart (TextEditorData data) { if (!data.CanEdit (data.Caret.Line)) return; var line = data.Document.GetLine (data.Caret.Line); using (var undo = data.OpenUndoGroup ()) { data.EnsureCaretIsNotVirtual (); int physColumn = data.Caret.Column - 1; var startLine = GetStartOfLineOffset (data, data.Caret.Location); data.Remove (startLine, (line.Offset + physColumn) - startLine); } data.Document.CommitLineUpdate (data.Caret.Line); }
protected void InsertCharacter(uint unicodeKey) { if (!textEditorData.CanEdit(Data.Caret.Line)) { return; } HideMouseCursor(); using (var undo = Document.OpenUndoGroup()) { textEditorData.DeleteSelectedText(textEditorData.IsSomethingSelected ? textEditorData.MainSelection.SelectionMode != SelectionMode.Block : true); char ch = (char)unicodeKey; if (!char.IsControl(ch) && textEditorData.CanEdit(Caret.Line)) { LineSegment line = Document.GetLine(Caret.Line); if (Caret.IsInInsertMode || Caret.Column >= line.EditableLength + 1) { string text = Caret.Column > line.EditableLength + 1 ? textEditorData.GetVirtualSpaces(Caret.Line, Caret.Column) + ch.ToString() : ch.ToString(); if (textEditorData.IsSomethingSelected && textEditorData.MainSelection.SelectionMode == SelectionMode.Block) { int length = 0; var visualInsertLocation = editor.LogicalToVisualLocation(Caret.Location); for (int lineNumber = textEditorData.MainSelection.MinLine; lineNumber <= textEditorData.MainSelection.MaxLine; lineNumber++) { LineSegment lineSegment = textEditorData.GetLine(lineNumber); int insertOffset = lineSegment.GetLogicalColumn(textEditorData, visualInsertLocation.Column) - 1; string textToInsert; if (lineSegment.EditableLength < insertOffset) { int visualLastColumn = lineSegment.GetVisualColumn(textEditorData, lineSegment.EditableLength + 1); int charsToInsert = visualInsertLocation.Column - visualLastColumn; int spaceCount = charsToInsert % editor.Options.TabSize; textToInsert = new string ('\t', (charsToInsert - spaceCount) / editor.Options.TabSize) + new string (' ', spaceCount) + text; insertOffset = lineSegment.EditableLength; } else { textToInsert = text; } length = textEditorData.Insert(lineSegment.Offset + insertOffset, textToInsert); } Caret.PreserveSelection = true; Caret.Column += length - 1; textEditorData.MainSelection.Lead = new DocumentLocation(textEditorData.MainSelection.Lead.Line, Caret.Column + 1); textEditorData.MainSelection.Anchor = new DocumentLocation(textEditorData.MainSelection.Anchor.Line, Caret.Column + 1); Document.CommitMultipleLineUpdate(textEditorData.MainSelection.MinLine, textEditorData.MainSelection.MaxLine); } else { int length = textEditorData.Insert(Caret.Offset, text); Caret.Column += length - 1; } } else { int length = textEditorData.Replace(Caret.Offset, 1, ch.ToString()); if (length > 1) { Caret.Offset += length - 1; } } // That causes unnecessary redraws: // bool autoScroll = Caret.AutoScrollToCaret; Caret.Column++; if (Caret.PreserveSelection) { Caret.PreserveSelection = false; } // Caret.AutoScrollToCaret = autoScroll; // if (autoScroll) // Editor.ScrollToCaret (); // Document.RequestUpdate (new LineUpdate (Caret.Line)); // Document.CommitDocumentUpdate (); } } Document.OptimizeTypedUndo(); }
public static void CaretLine (TextEditorData data) { if (data.Document.LineCount <= 1 || !data.CanEdit (data.Caret.Line)) return; LineSegment line = data.Document.GetLine (data.Caret.Line); data.Remove (line.Offset, line.LengthIncludingDelimiter); data.Caret.Column = DocumentLocation.MinColumn; }
static int PasteFrom (Clipboard clipboard, TextEditorData data, bool preserveSelection, int insertionOffset, bool preserveState) { int result = -1; if (!data.CanEdit (data.Document.OffsetToLineNumber (insertionOffset))) return result; if (clipboard.WaitIsTargetAvailable (CopyOperation.MD_ATOM)) { clipboard.RequestContents (CopyOperation.MD_ATOM, delegate(Clipboard clp, SelectionData selectionData) { if (selectionData.Length > 0) { byte[] selBytes = selectionData.Data; string text = System.Text.Encoding.UTF8.GetString (selBytes, 1, selBytes.Length - 1); bool pasteBlock = (selBytes [0] & 1) == 1; bool pasteLine = (selBytes [0] & 2) == 2; using (var undo = data.OpenUndoGroup ()) { if (preserveSelection && data.IsSomethingSelected) data.DeleteSelectedText (); data.Caret.PreserveSelection = true; if (pasteBlock) { string[] lines = text.Split ('\r'); int lineNr = data.Document.OffsetToLineNumber (insertionOffset); int col = insertionOffset - data.Document.GetLine (lineNr).Offset; int visCol = data.Document.GetLine (lineNr).GetVisualColumn (data, col); DocumentLine curLine; int lineCol = col; result = 0; for (int i = 0; i < lines.Length; i++) { while (data.Document.LineCount <= lineNr + i) { data.Insert (data.Document.TextLength, Environment.NewLine); result += Environment.NewLine.Length; } curLine = data.Document.GetLine (lineNr + i); if (lines [i].Length > 0) { lineCol = curLine.GetLogicalColumn (data, visCol); if (curLine.Length + 1 < lineCol) { result += lineCol - curLine.Length; data.Insert (curLine.Offset + curLine.Length, new string (' ', lineCol - curLine.Length)); } data.Insert (curLine.Offset + lineCol, lines [i]); result += lines [i].Length; } if (!preserveState) data.Caret.Offset = curLine.Offset + lineCol + lines [i].Length; } } else if (pasteLine) { result = text.Length; DocumentLine curLine = data.Document.GetLine (data.Caret.Line); data.Insert (curLine.Offset, text + data.EolMarker); } else { int offset = data.Caret.Offset; data.InsertAtCaret (text); data.PasteText (offset, text, data.Caret.Offset - offset); } /* data.MainSelection = new Selection (data.Document.OffsetToLocation (insertionOffset), data.Caret.Location, lines.Length > 1 ? SelectionMode.Block : SelectionMode.Normal);*/ if (!preserveState) data.ClearSelection (); data.Caret.PreserveSelection = false; } } }); // we got MD_ATOM text - no need to request text. (otherwise buffer may get copied twice). return result; } if (result < 0 && clipboard.WaitIsTextAvailable ()) { clipboard.RequestText (delegate(Clipboard clp, string text) { if (string.IsNullOrEmpty (text)) return; using (var undo = data.OpenUndoGroup ()) { int caretPos = data.Caret.Offset; if (data.IsSomethingSelected && data.MainSelection.SelectionMode == SelectionMode.Block) { data.Caret.PreserveSelection = true; data.DeleteSelectedText (false); int textLength = 0; int minLine = data.MainSelection.MinLine; int maxLine = data.MainSelection.MaxLine; var visualInsertLocation = data.LogicalToVisualLocation (data.Caret.Location); for (int lineNumber = minLine; lineNumber <= maxLine; lineNumber++) { DocumentLine lineSegment = data.GetLine (lineNumber); int insertOffset = lineSegment.GetLogicalColumn (data, visualInsertLocation.Column) - 1; if (lineSegment.Length < insertOffset) { int visualLastColumn = lineSegment.GetVisualColumn (data, lineSegment.Length + 1); int charsToInsert = visualInsertLocation.Column - visualLastColumn; int spaceCount = charsToInsert % data.Options.TabSize; string textToInsert = new string ('\t', (charsToInsert - spaceCount) / data.Options.TabSize) + new string (' ', spaceCount) + text; insertOffset = lineSegment.Length; int insertedChars = data.Insert (lineSegment.Offset + insertOffset, textToInsert); data.PasteText (lineSegment.Offset + insertOffset, textToInsert, insertedChars); } else { textLength = data.Insert (lineSegment.Offset + insertOffset, text); data.PasteText (lineSegment.Offset + insertOffset, text, textLength); } } data.MainSelection.Anchor = new DocumentLocation (System.Math.Max (DocumentLocation.MinLine, data.Caret.Line == minLine ? maxLine : minLine), System.Math.Max (DocumentLocation.MinColumn, data.Caret.Column - textLength)); data.MainSelection.Lead = new DocumentLocation (data.Caret.Line, data.Caret.Column); data.Caret.PreserveSelection = false; data.Document.CommitMultipleLineUpdate (data.MainSelection.MinLine, data.MainSelection.MaxLine); } else { TextSegment selection = data.SelectionRange; if (preserveSelection && data.IsSomethingSelected) data.DeleteSelectedText (); data.Caret.PreserveSelection = true; //int oldLine = data.Caret.Line; int textLength = data.Insert (insertionOffset, text); result = textLength; if (data.IsSomethingSelected && data.SelectionRange.Offset >= insertionOffset) data.SelectionRange = new TextSegment (data.SelectionRange.Offset + textLength, data.SelectionRange.Length); if (data.IsSomethingSelected && data.MainSelection.GetAnchorOffset (data) >= insertionOffset) data.MainSelection.Anchor = data.Document.OffsetToLocation (data.MainSelection.GetAnchorOffset (data) + textLength); data.Caret.PreserveSelection = false; if (!preserveState) { } else { if (!selection.IsInvalid) { int offset = selection.Offset; if (offset >= insertionOffset) offset += textLength; data.SelectionRange = new TextSegment (offset, selection.Length); } } data.PasteText (insertionOffset, text, textLength); } } }); } return result; }
protected void InsertCharacter(uint unicodeKey) { if (!textEditorData.CanEdit(Data.Caret.Line)) { return; } HideMouseCursor(); Document.BeginAtomicUndo(); if (textEditorData.IsSomethingSelected && textEditorData.MainSelection.SelectionMode == SelectionMode.Block) { textEditorData.Caret.PreserveSelection = true; if (!textEditorData.MainSelection.IsDirty) { textEditorData.DeleteSelectedText(false); textEditorData.MainSelection.IsDirty = true; } } else { textEditorData.DeleteSelectedText(); } char ch = (char)unicodeKey; if (!char.IsControl(ch) && textEditorData.CanEdit(Caret.Line)) { LineSegment line = Document.GetLine(Caret.Line); if (Caret.IsInInsertMode || Caret.Column >= line.EditableLength) { string text = Caret.Column > line.EditableLength ? textEditorData.GetVirtualSpaces(Caret.Line, Caret.Column) + ch.ToString() : ch.ToString(); if (textEditorData.IsSomethingSelected && textEditorData.MainSelection.SelectionMode == SelectionMode.Block) { int length = 0; for (int lineNumber = textEditorData.MainSelection.MinLine; lineNumber <= textEditorData.MainSelection.MaxLine; lineNumber++) { length = textEditorData.Insert(textEditorData.Document.GetLine(lineNumber).Offset + Caret.Column, text); } Caret.Column += length - 1; textEditorData.MainSelection.Lead = new DocumentLocation(textEditorData.MainSelection.Lead.Line, Caret.Column + 1); textEditorData.MainSelection.IsDirty = true; Document.CommitMultipleLineUpdate(textEditorData.MainSelection.MinLine, textEditorData.MainSelection.MaxLine); } else { int length = textEditorData.Insert(Caret.Offset, text); Caret.Column += length - 1; } } else { int length = textEditorData.Replace(Caret.Offset, 1, ch.ToString()); if (length > 1) { Caret.Offset += length - 1; } } // That causes unnecessary redraws: // bool autoScroll = Caret.AutoScrollToCaret; Caret.Column++; // Caret.AutoScrollToCaret = autoScroll; // if (autoScroll) // Editor.ScrollToCaret (); // Document.RequestUpdate (new LineUpdate (Caret.Line)); // Document.CommitDocumentUpdate (); } if (textEditorData.IsSomethingSelected && textEditorData.MainSelection.SelectionMode == SelectionMode.Block) { textEditorData.Caret.PreserveSelection = false; } Document.EndAtomicUndo(); Document.OptimizeTypedUndo(); }
public static void ToggleCase(TextEditorData data) { if (data.IsSomethingSelected) { if (!data.CanEditSelection) return; StringBuilder sb = new StringBuilder (data.SelectedText); for (int i = 0; i < sb.Length; i++) { char ch = sb [i]; if (Char.IsLower (ch)) sb [i] = Char.ToUpper (ch); else if (Char.IsUpper (ch)) sb [i] = Char.ToLower (ch); } data.Replace (data.SelectionRange.Offset, data.SelectionRange.Length, sb.ToString ()); } else if (data.CanEdit (data.Caret.Line)) { char ch = data.Document.GetCharAt (data.Caret.Offset); if (Char.IsLower (ch)) ch = Char.ToUpper (ch); else if (Char.IsUpper (ch)) ch = Char.ToLower (ch); var caretOffset = data.Caret.Offset; int length = data.Replace (caretOffset, 1, new string (ch, 1)); DocumentLine seg = data.Document.GetLine (data.Caret.Line); if (data.Caret.Column < seg.Length) data.Caret.Offset = caretOffset + length; } }
protected void InsertCharacter(uint unicodeKey) { if (!textEditorData.CanEdit(Data.Caret.Line)) { return; } HideMouseCursor(); using (var undo = Document.OpenUndoGroup()) { if (textEditorData.IsSomethingSelected && textEditorData.Options.EnableSelectionWrappingKeys && IsSpecialKeyForSelection(unicodeKey)) { textEditorData.SelectionSurroundingProvider.HandleSpecialSelectionKey(textEditorData, unicodeKey); return; } textEditorData.DeleteSelectedText( textEditorData.IsSomethingSelected ? textEditorData.MainSelection.SelectionMode != SelectionMode.Block : true); // Needs to be called after delete text, delete text handles virtual caret postitions itself, // but afterwards the virtual position may need to be restored. textEditorData.EnsureCaretIsNotVirtual(); char ch = (char)unicodeKey; if (!char.IsControl(ch) && textEditorData.CanEdit(Caret.Line)) { DocumentLine line = Document.GetLine(Caret.Line); if (Caret.IsInInsertMode || Caret.Column >= line.Length + 1) { string text = ch.ToString(); if (textEditorData.IsSomethingSelected && textEditorData.MainSelection.SelectionMode == SelectionMode.Block) { var visualInsertLocation = textEditorData.LogicalToVisualLocation(Caret.Location); var selection = textEditorData.MainSelection; Caret.PreserveSelection = true; for (int lineNumber = selection.MinLine; lineNumber <= selection.MaxLine; lineNumber++) { DocumentLine lineSegment = textEditorData.GetLine(lineNumber); int insertOffset = lineSegment.GetLogicalColumn(textEditorData, visualInsertLocation.Column) - 1; string textToInsert; if (lineSegment.Length < insertOffset) { int visualLastColumn = lineSegment.GetVisualColumn(textEditorData, lineSegment.Length + 1); int charsToInsert = visualInsertLocation.Column - visualLastColumn; int spaceCount = charsToInsert % editor.Options.TabSize; textToInsert = new string ('\t', (charsToInsert - spaceCount) / editor.Options.TabSize) + new string (' ', spaceCount) + text; insertOffset = lineSegment.Length; } else { textToInsert = text; } textEditorData.Insert(lineSegment.Offset + insertOffset, textToInsert); } var visualColumn = textEditorData.GetLine(Caret.Location.Line).GetVisualColumn(textEditorData, Caret.Column); textEditorData.MainSelection = new Selection( new DocumentLocation(selection.Anchor.Line, textEditorData.GetLine(selection.Anchor.Line).GetLogicalColumn(textEditorData, visualColumn)), new DocumentLocation(selection.Lead.Line, textEditorData.GetLine(selection.Lead.Line).GetLogicalColumn(textEditorData, visualColumn)), SelectionMode.Block ); Document.CommitMultipleLineUpdate(textEditorData.MainSelection.MinLine, textEditorData.MainSelection.MaxLine); } else { textEditorData.Insert(Caret.Offset, text); } } else { textEditorData.Replace(Caret.Offset, 1, ch.ToString()); } // That causes unnecessary redraws: // bool autoScroll = Caret.AutoScrollToCaret; // Caret.Column++; if (Caret.PreserveSelection) { Caret.PreserveSelection = false; } // Caret.AutoScrollToCaret = autoScroll; // if (autoScroll) // Editor.ScrollToCaret (); // Document.RequestUpdate (new LineUpdate (Caret.Line)); // Document.CommitDocumentUpdate (); } } Document.OptimizeTypedUndo(); }
public static void CaretLine (TextEditorData data) { if (data.Document.LineCount <= 1 || !data.CanEdit (data.Caret.Line)) return; using (var undo = data.OpenUndoGroup ()) { if (data.IsSomethingSelected) { var startLine = data.GetLine (data.MainSelection.Start.Line); var endLine = data.GetLine (data.MainSelection.End.Line); data.Remove (startLine.Offset, endLine.EndOffsetIncludingDelimiter - startLine.Offset); return; } var start = GetStartOfLineOffset (data, data.Caret.Location); var end = GetEndOfLineOffset (data, data.Caret.Location); data.Remove (start, end - start); data.Caret.Column = DocumentLocation.MinColumn; } }
public static void CaretLineToEnd (TextEditorData data) { if (!data.CanEdit (data.Caret.Line)) return; LineSegment line = data.Document.GetLine (data.Caret.Line); if (data.Caret.Column == line.EditableLength) { // Nothing after the cursor, delete the end-of-line sequence data.Remove (line.Offset + data.Caret.Column, line.Length - data.Caret.Column); } else { // Delete from cursor position to the end of the line data.Remove (line.Offset + data.Caret.Column, line.EditableLength - data.Caret.Column); } data.Document.CommitLineUpdate (data.Caret.Line); }
static int PasteFrom (Clipboard clipboard, TextEditorData data, bool preserveSelection, int insertionOffset, bool preserveState) { int result = -1; if (!data.CanEdit (data.Document.OffsetToLineNumber (insertionOffset))) return result; if (clipboard.WaitIsTargetAvailable (CopyOperation.MD_ATOM)) { clipboard.RequestContents (CopyOperation.MD_ATOM, delegate(Clipboard clp, SelectionData selectionData) { if (selectionData.Length > 0) { byte[] selBytes = selectionData.Data; var upperBound = System.Math.Max (0, System.Math.Min (selBytes [1], selBytes.Length - 2)); byte[] copyData = new byte[upperBound]; Array.Copy (selBytes, 2, copyData, 0, copyData.Length); var rawTextOffset = 1 + 1 + copyData.Length; string text = Encoding.UTF8.GetString (selBytes, rawTextOffset, selBytes.Length - rawTextOffset); bool pasteBlock = (selBytes [0] & 1) == 1; bool pasteLine = (selBytes [0] & 2) == 2; if (pasteBlock) { using (var undo = data.OpenUndoGroup ()) { var version = data.Document.Version; if (!preserveSelection) data.DeleteSelectedText (!data.IsSomethingSelected || data.MainSelection.SelectionMode != SelectionMode.Block); int startLine = data.Caret.Line; data.EnsureCaretIsNotVirtual (); insertionOffset = version.MoveOffsetTo (data.Document.Version, insertionOffset); data.Caret.PreserveSelection = true; var lines = new List<string> (); int offset = 0; while (true) { var delimiter = LineSplitter.NextDelimiter (text, offset); if (delimiter.IsInvalid) break; int delimiterEndOffset = delimiter.EndOffset; lines.Add (text.Substring (offset, delimiter.Offset - offset)); offset = delimiterEndOffset; } if (offset < text.Length) lines.Add (text.Substring (offset, text.Length - offset)); int lineNr = data.Document.OffsetToLineNumber (insertionOffset); int col = insertionOffset - data.Document.GetLine (lineNr).Offset; int visCol = data.Document.GetLine (lineNr).GetVisualColumn (data, col); DocumentLine curLine; int lineCol = col; result = 0; for (int i = 0; i < lines.Count; i++) { while (data.Document.LineCount <= lineNr + i) { data.Insert (data.Document.TextLength, Environment.NewLine); result += Environment.NewLine.Length; } curLine = data.Document.GetLine (lineNr + i); if (lines [i].Length > 0) { lineCol = curLine.GetLogicalColumn (data, visCol); if (curLine.Length + 1 < lineCol) { result += lineCol - curLine.Length; data.Insert (curLine.Offset + curLine.Length, new string (' ', lineCol - curLine.Length)); } data.Insert (curLine.Offset + lineCol, lines [i]); result += lines [i].Length; } if (!preserveState) data.Caret.Offset = curLine.Offset + lineCol + lines [i].Length; } if (!preserveState) data.ClearSelection (); data.FixVirtualIndentation (startLine); data.Caret.PreserveSelection = false; } } else if (pasteLine) { using (var undo = data.OpenUndoGroup ()) { if (!preserveSelection) data.DeleteSelectedText (!data.IsSomethingSelected || data.MainSelection.SelectionMode != SelectionMode.Block); data.EnsureCaretIsNotVirtual (); data.Caret.PreserveSelection = true; result = text.Length; DocumentLine curLine = data.Document.GetLine (data.Caret.Line); result = PastePlainText (data, curLine.Offset, text + data.EolMarker, preserveSelection, copyData); if (!preserveState) data.ClearSelection (); data.Caret.PreserveSelection = false; data.FixVirtualIndentation (curLine.LineNumber); } } else { result = PastePlainText (data, insertionOffset, text, preserveSelection, copyData); } } }); // we got MD_ATOM text - no need to request text. (otherwise buffer may get copied twice). return result; } if (result < 0 && clipboard.WaitIsTextAvailable ()) { clipboard.RequestText (delegate(Clipboard clp, string text) { if (string.IsNullOrEmpty (text)) return; result = PastePlainText (data, insertionOffset, text, preserveSelection); }); } return result; }
static int PasteFrom (Clipboard clipboard, TextEditorData data, bool preserveSelection, int insertionOffset, bool preserveState) { int result = -1; if (!data.CanEdit (data.Document.OffsetToLineNumber (insertionOffset))) return result; if (clipboard.WaitIsTargetAvailable (CopyOperation.MD_ATOM)) { clipboard.RequestContents (CopyOperation.MD_ATOM, delegate(Clipboard clp, SelectionData selectionData) { if (selectionData.Length > 0) { byte[] selBytes = selectionData.Data; string text = System.Text.Encoding.UTF8.GetString (selBytes, 1, selBytes.Length - 1); bool pasteBlock = (selBytes [0] & 1) == 1; bool pasteLine = (selBytes [0] & 2) == 2; // var clearSelection = data.IsSomethingSelected ? data.MainSelection.SelectionMode != SelectionMode.Block : true; if (pasteBlock) { using (var undo = data.OpenUndoGroup ()) { var version = data.Document.Version; data.DeleteSelectedText (!data.IsSomethingSelected || data.MainSelection.SelectionMode != SelectionMode.Block); data.EnsureCaretIsNotVirtual (); insertionOffset = version.MoveOffsetTo (data.Document.Version, insertionOffset); data.Caret.PreserveSelection = true; string[] lines = text.Split ('\r'); int lineNr = data.Document.OffsetToLineNumber (insertionOffset); int col = insertionOffset - data.Document.GetLine (lineNr).Offset; int visCol = data.Document.GetLine (lineNr).GetVisualColumn (data, col); DocumentLine curLine; int lineCol = col; result = 0; for (int i = 0; i < lines.Length; i++) { while (data.Document.LineCount <= lineNr + i) { data.Insert (data.Document.TextLength, Environment.NewLine); result += Environment.NewLine.Length; } curLine = data.Document.GetLine (lineNr + i); if (lines [i].Length > 0) { lineCol = curLine.GetLogicalColumn (data, visCol); if (curLine.Length + 1 < lineCol) { result += lineCol - curLine.Length; data.Insert (curLine.Offset + curLine.Length, new string (' ', lineCol - curLine.Length)); } data.Insert (curLine.Offset + lineCol, lines [i]); result += lines [i].Length; } if (!preserveState) data.Caret.Offset = curLine.Offset + lineCol + lines [i].Length; } if (!preserveState) data.ClearSelection (); data.Caret.PreserveSelection = false; } } else if (pasteLine) { using (var undo = data.OpenUndoGroup ()) { data.DeleteSelectedText (!data.IsSomethingSelected || data.MainSelection.SelectionMode != SelectionMode.Block); data.EnsureCaretIsNotVirtual (); data.Caret.PreserveSelection = true; result = text.Length; DocumentLine curLine = data.Document.GetLine (data.Caret.Line); data.Insert (curLine.Offset, text + data.EolMarker); if (!preserveState) data.ClearSelection (); data.Caret.PreserveSelection = false; } } else { result = PastePlainText (data, insertionOffset, text); } } }); // we got MD_ATOM text - no need to request text. (otherwise buffer may get copied twice). return result; } if (result < 0 && clipboard.WaitIsTextAvailable ()) { clipboard.RequestText (delegate(Clipboard clp, string text) { if (string.IsNullOrEmpty (text)) return; using (var undo = data.OpenUndoGroup ()) { result = PastePlainText (data, insertionOffset, text); } }); } return result; }
static int PasteFrom(TextEditorData data, bool preserveSelection, int insertionOffset, bool preserveState) { int result = -1; if (!data.CanEdit(data.Document.OffsetToLineNumber(insertionOffset))) { return(result); } if (Clipboard.ContainsData <byte[]>()) { byte[] selBytes = Clipboard.GetData <byte[]>(); var upperBound = System.Math.Max(0, System.Math.Min(selBytes[1], selBytes.Length - 2)); byte[] copyData = new byte[upperBound]; Array.Copy(selBytes, 2, copyData, 0, copyData.Length); var rawTextOffset = 1 + 1 + copyData.Length; string text = Encoding.UTF8.GetString(selBytes, rawTextOffset, selBytes.Length - rawTextOffset); bool pasteBlock = (selBytes[0] & 1) == 1; bool pasteLine = (selBytes[0] & 2) == 2; if (pasteBlock) { using (var undo = data.OpenUndoGroup()) { var version = data.Document.Version; if (!preserveSelection) { data.DeleteSelectedText(!data.IsSomethingSelected || data.MainSelection.SelectionMode != SelectionMode.Block); } int startLine = data.Caret.Line; data.EnsureCaretIsNotVirtual(); insertionOffset = version.MoveOffsetTo(data.Document.Version, insertionOffset); data.Caret.PreserveSelection = true; var lines = new List <string>(); int offset = 0; while (true) { var delimiter = LineSplitter.NextDelimiter(text, offset); if (delimiter.IsInvalid) { break; } int delimiterEndOffset = delimiter.EndOffset; lines.Add(text.Substring(offset, delimiter.Offset - offset)); offset = delimiterEndOffset; } if (offset < text.Length) { lines.Add(text.Substring(offset, text.Length - offset)); } int lineNr = data.Document.OffsetToLineNumber(insertionOffset); int col = insertionOffset - data.Document.GetLine(lineNr).Offset; int visCol = data.Document.GetLine(lineNr).GetVisualColumn(data, col); DocumentLine curLine; int lineCol = col; result = 0; for (int i = 0; i < lines.Count; i++) { while (data.Document.LineCount <= lineNr + i) { data.Insert(data.Document.TextLength, Environment.NewLine); result += Environment.NewLine.Length; } curLine = data.Document.GetLine(lineNr + i); if (lines[i].Length > 0) { lineCol = curLine.GetLogicalColumn(data, visCol); if (curLine.Length + 1 < lineCol) { result += lineCol - curLine.Length; data.Insert(curLine.Offset + curLine.Length, new string(' ', lineCol - curLine.Length)); } data.Insert(curLine.Offset + lineCol, lines[i]); result += lines[i].Length; } if (!preserveState) { data.Caret.Offset = curLine.Offset + lineCol + lines[i].Length; } } if (!preserveState) { data.ClearSelection(); } data.FixVirtualIndentation(startLine); data.Caret.PreserveSelection = false; } } else if (pasteLine) { using (var undo = data.OpenUndoGroup()) { if (!preserveSelection) { data.DeleteSelectedText(!data.IsSomethingSelected || data.MainSelection.SelectionMode != SelectionMode.Block); } data.EnsureCaretIsNotVirtual(); data.Caret.PreserveSelection = true; result = text.Length; DocumentLine curLine = data.Document.GetLine(data.Caret.Line); result = PastePlainText(data, curLine.Offset, text + data.EolMarker, preserveSelection, copyData); if (!preserveState) { data.ClearSelection(); } data.Caret.PreserveSelection = false; data.FixVirtualIndentation(curLine.LineNumber); } } else { result = PastePlainText(data, insertionOffset, text, preserveSelection, copyData); } } else if (Clipboard.ContainsData(TransferDataType.Text)) { var text = Clipboard.GetText(); result = PastePlainText(data, insertionOffset, text, preserveState); } return(result); }