public void ProcessKey(object sender, int index, ConsoleKeyInfo key) { if (key.Key == 0) { return; // This causes an fun bug (CTRL+M) } if (key.Modifiers == ConsoleModifiers.Control) { // Weird character to hide if (Console.CursorLeft > 0) { Console.CursorLeft -= 1; Console.Write(" "); } bool returnAfterSwitch = true; switch (key.Key) { case ConsoleKey.S: SaveToFile(); return; case ConsoleKey.Backspace: //contentBuffer[index] = contentBuffer[index].TrimEnd(); if (CursorLeft == 0) { returnAfterSwitch = false; break; } int removeStart = CursorLeft; CharType rmt = CharType.Special; // Default if (char.IsLetter(contentBuffer[index][CursorLeft - 1])) { rmt = CharType.Word; } else if (char.IsWhiteSpace(contentBuffer[index][CursorLeft - 1])) { rmt = CharType.Space; } if (rmt == CharType.Special) { contentBuffer[index] = contentBuffer[index].Remove(CursorLeft - 1, 1); CursorLeft = contentBuffer[index].Length; FileIsSaved = false; return; } for (int i = CursorLeft - 1; i >= 0; i--) { if ((char.IsWhiteSpace(contentBuffer[index][i]) && rmt == CharType.Space) || (char.IsLetter(contentBuffer[index][i]) && rmt == CharType.Word)) { removeStart = i; } else { break; } } contentBuffer[index] = contentBuffer[index].Remove(removeStart, CursorLeft - removeStart); CursorLeft = removeStart; FileIsSaved = false; return; case ConsoleKey.O: OpenFileDialog openFile = new OpenFileDialog(); openFile.Multiselect = true; if (openFile.ShowDialog() == DialogResult.OK) { for (int i = 0; i < openFile.FileNames.Length; i++) { Parent.OpenFileEditor(openFile.FileNames[i]); Parent.SetCurrentEditor(Parent.Editors.Count - 1); } if (!FileIsSaved && Filename == Settings.NewFileName && contentBuffer.Count == 1 && string.IsNullOrEmpty(contentBuffer[0].Trim())) { Parent.Editors.Remove(this); Parent.SetCurrentEditor(0); } Parent.DrawTabs(); } return; case ConsoleKey.N: // SOMETHING IS VERY WRONG HERE Parent.NewFileEditor(); // Switch to that editor Parent.SetCurrentEditor(Parent.Editors.Count - 1); return; case ConsoleKey.R: // Run file if (LanguageSyntax.IsExecutable()) { if (!FileIsSaved) { SaveToFile(); FileIsSaved = true; } FileInfo file = new FileInfo(Filepath); Executor executor = new Executor(LanguageSyntax.ExecutionArguments(Filepath), Filename, file.Directory.FullName); executor.Start(); Parent.Draw(); DrawAllLines(); } return; case ConsoleKey.V: string rawText = Clipboard.GetText(); string[] texts = rawText.Replace("\r", "").Replace("\t", Settings.TabSize).Split('\n'); // Extract all the text that's on the right side of the cursor string rhsText = ""; if (CursorLeft < contentBuffer[CursorTop].Length) { rhsText = contentBuffer[CursorTop].Substring(CursorLeft); contentBuffer[CursorTop] = contentBuffer[CursorTop].Remove(CursorLeft); } int j; for (j = 0; j < texts.Length; j++) { if (j == 0) { contentBuffer[CursorTop] = contentBuffer[CursorTop].Insert(CursorLeft, texts[j]); } else { contentBuffer.Insert(CursorTop + j, texts[j]); } } j--; CursorLeft = contentBuffer[CursorTop + j].Length; contentBuffer[CursorTop + j] += rhsText; CursorTop = CursorTop + j; DrawAllLines(); return; case ConsoleKey.Z: // Undo return; case ConsoleKey.Y: // Redo return; case ConsoleKey.RightArrow: if (CursorLeft == contentBuffer[index].Length) { if (CursorTop < contentBuffer.Count - 1) { CursorTop++; CursorLeft = 0; } } else { CharType jct = CharType.Special; // Default if (char.IsLetter(contentBuffer[index][CursorLeft])) { jct = CharType.Word; } else if (char.IsWhiteSpace(contentBuffer[index][CursorLeft])) { jct = CharType.Space; } if (jct == CharType.Special) { if (CursorLeft < contentBuffer[index].Length) { CursorLeft++; } return; } for (int i = CursorLeft; i < contentBuffer[index].Length; i++) { char c = contentBuffer[index][i]; if ((char.IsWhiteSpace(c) && jct == CharType.Space) || (char.IsLetter(c) && jct == CharType.Word)) { CursorLeft++; } else { return; } } } return; case ConsoleKey.LeftArrow: if (CursorLeft == 0) { if (CursorTop > 0) { CursorTop--; CursorLeft = contentBuffer[index - 1].Length; DrawLine(index); // Draw the "previous" line } } else { CharType jct = CharType.Special; // Default if (char.IsLetter(contentBuffer[index][CursorLeft - 1])) { jct = CharType.Word; } else if (char.IsWhiteSpace(contentBuffer[index][CursorLeft - 1])) { jct = CharType.Space; } if (jct == CharType.Special) { if (CursorLeft > 0) { CursorLeft--; } return; } for (int i = CursorLeft - 1; i >= 0; i--) { char c = contentBuffer[index][i]; if ((char.IsWhiteSpace(c) && jct == CharType.Space) || (char.IsLetter(c) && jct == CharType.Word)) { CursorLeft--; } else { return; } } } return; // Pass through case ConsoleKey.Enter: case ConsoleKey.Delete: case ConsoleKey.DownArrow: case ConsoleKey.UpArrow: returnAfterSwitch = false; break; } if (returnAfterSwitch) { return; } } if (key.Modifiers == ConsoleModifiers.Alt) { if (char.IsDigit(key.KeyChar)) { // Change Tab try { int tabIndex = int.Parse(key.KeyChar.ToString()) - 1; Program.ParentWindow.SetCurrentEditor(tabIndex); } catch { } return; } else if (key.Key == ConsoleKey.F4) { // Prompt to Close current tab or close editor Parent.CloseCurrentTab(); } } if (key.Key == ConsoleKey.Enter) { int prevLineIndexLen = LinesLength; FileIsSaved = false; string leftHandSide = contentBuffer[index].Substring(CursorLeft, contentBuffer[index].Length - CursorLeft); contentBuffer[index] = contentBuffer[index].Remove(CursorLeft, contentBuffer[index].Length - CursorLeft); bool autoIndent = LanguageSyntax.IndentNextLine(contentBuffer[index]); contentBuffer.Insert(CursorTop + 1, (autoIndent ? Settings.TabSize : "") + leftHandSide); CursorTop++; if (autoIndent) { CursorLeft = Settings.TabSize.Length; } else { CursorLeft = 0; } if (prevLineIndexLen != LinesLength) { Parent.DrawTabs(); DrawAllLines(); } else { DrawAllLines(CursorTop - 1); } return; } else if (key.Key == ConsoleKey.UpArrow) { if (CursorTop > 0) { if (CursorLeft > contentBuffer[index - 1].Length) { CursorLeft = contentBuffer[index - 1].Length; } CursorTop--; DrawLine(index); // Draw the previous line } return; } else if (key.Key == ConsoleKey.DownArrow) { if (CursorTop < contentBuffer.Count - 1) { if (CursorLeft > contentBuffer[index + 1].Length) { CursorLeft = contentBuffer[index + 1].Length; } CursorTop++; DrawLine(index); // Draw the previous line } return; } else if (key.Key == ConsoleKey.LeftArrow) { if (CursorLeft > 0) { CursorLeft -= 1; } else if (CursorTop > 0 && CursorLeft == 0) { CursorTop--; CursorLeft = contentBuffer[index - 1].Length; DrawLine(index); // Draw the previous line } return; } else if (key.Key == ConsoleKey.RightArrow) { if (CursorLeft < contentBuffer[index].Length) { CursorLeft += 1; } else if (CursorTop + 1 < contentBuffer.Count) { CursorTop++; CursorLeft = 0; } return; } else if (key.Key == ConsoleKey.Backspace) { FileIsSaved = false; if (CursorLeft == 0 && CursorTop > 0) { int prevLinesLength = LinesLength; int lastLineLen = contentBuffer[contentBuffer.Count - 1].Length; CursorLeft = contentBuffer[index - 1].Length; contentBuffer[index - 1] = contentBuffer[index - 1] + contentBuffer[index]; contentBuffer.RemoveAt(index); CursorTop--; Parent.DrawTabs(); if (prevLinesLength == LinesLength) { DrawAllLines(CursorTop); } else { DrawAllLines(); } // Clear the previous line in chunks at the time ClearLine(contentBuffer.Count, contentBuffer[index - 1].Length); return; } if (contentBuffer[index].Length == 0 && CursorTop > 0) { contentBuffer.RemoveAt(index); CursorTop--; Parent.Draw(); DrawAllLines(); return; } if (CursorLeft > 0) { contentBuffer[index] = contentBuffer[index].Remove(CursorLeft - 1, 1); } if (CursorLeft > 0) { CursorLeft--; } return; } else if (key.Key == ConsoleKey.Delete) { int prevLinesLength = LinesLength; FileIsSaved = false; if (CursorLeft == contentBuffer[index].Length && CursorTop < contentBuffer.Count - 1) { contentBuffer[index] = contentBuffer[index] + contentBuffer[index + 1]; contentBuffer.RemoveAt(index + 1); if (prevLinesLength != LinesLength) { Parent.DrawTabs(); DrawAllLines(); } else { DrawAllLines(CursorTop); } ClearLine(contentBuffer.Count, contentBuffer[index].Length); return; } if (CursorLeft < contentBuffer[index].Length) { contentBuffer[index] = contentBuffer[index].Remove(CursorLeft, 1); } return; } else if (key.Key == ConsoleKey.Tab) { FileIsSaved = false; contentBuffer[index] = contentBuffer[index].Insert(CursorLeft, Settings.TabSize); CursorLeft += Settings.TabSize.Length; return; } else if (key.Key == ConsoleKey.Escape) { Parent.ExitProgram(); return; } else if (key.Key == ConsoleKey.PageUp) { CursorLeft = 0; CursorTop = 0; DrawLine(index); // Draw previous line return; } else if (key.Key == ConsoleKey.PageDown) { CursorLeft = contentBuffer[contentBuffer.Count - 1].Length; CursorTop = contentBuffer.Count - 1; DrawLine(index); // Draw previous line return; } contentBuffer[index] = contentBuffer[index].Insert(CursorLeft, key.KeyChar.ToString()); CursorLeft++; FileIsSaved = false; }
static void Main(string[] args) { // Initialize a new parent window ParentWindow = new Component.ParentWindow(); Settings.InitialBufferSize = new System.Drawing.Size(Console.BufferWidth, Console.BufferWidth); //args = new string[0]; //args = new[] { "/open" }; //args = new[] { "/open", @"C:\Users\ewr0327\Desktop\myTestProg.py" }; //args = new[] { "/open", "exampleFile.c", "exampleFile2.py" }; //args = new[] { "exampleFile.c", "exampleFile2.py", "/help", "open" }; Arguments arguments = Arguments.Parse(args); if (arguments.FindPattern("new") || arguments.Length == 0) { // Create a tmp file somewhere and later save/remove it on exit if (arguments.Length == 0) { ParentWindow.NewFileEditor(); } else if (arguments["new"].Count == 0) { ParentWindow.NewFileEditor(); } else if (arguments["new"].Count > 0) { for (int i = 0; i < arguments["new"].Count; i++) { string filepath = arguments["new"][i]; string filename = Component.ParentWindow.ParseFileName(filepath); ParentWindow.NewFileEditor(filename, filepath, Component.Editor.DetectLanguageSyntax(filepath)); } } } else // If it's only filepaths passed with or without /open infront { if (arguments.FindPattern("open")) { if (arguments["open"].Count == 0) { Console.WriteLine("Missing file(s) to open!"); return; } for (int i = 0; i < arguments["open"].Count; i++) { ParentWindow.OpenFileEditor(arguments["open"][i]); } } else { for (int i = 0; i < arguments.KeylessArguments.Count; i++) { string filename = Component.ParentWindow.ParseFileName(arguments[i]); if (File.Exists(arguments[i])) { ParentWindow.OpenFileEditor(arguments[i]); } else { ParentWindow.NewFileEditor(filename, arguments[i], Component.Editor.DetectLanguageSyntax(filename)); } } } } // Setup console SetupConsole(); // Start editor ParentWindow.Start(); }