Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Typewriter.NET Text Editor

0.00/5 (No votes)
9 Nov 2018 1  
How to write your Notepad

Introduction

I wanted to develop a text editor that didn't require too much effort for using and learning. The result is several simple principles:

  • Editor works without mouse (and without arrows in vi-mode)
  • No monotonic actions required due to multiple cursors:

    vi-mode, macroses

  • Allow cursor jumping in the direction of gaze (press space in vi-mode, press char at cursor, then showing chars)
  • Uses the simple concept of the current folder instead of the project
  • Easy searching how to do something by Ctrl+Shift+P. And using full-text searching inside help
  • External compiler or build script can be appended by only one line in config
  • Settings via text files changing or setting values from the command dialog (such parameters are written without the value attribute in the config), which allows not to search for them in the interface

Using the Code

Typewriter.NET is useful to develop itself. To build and run Typewriter.NET:

  • Open typewriter-net as current folder in editor (F4).
  • Press F5.
  • This shortcut runs command in config:
    <item name="f5Command" 
    
    value="!c:\Windows\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe /verbosity:m 
    /p:Configuration=Release /target:tw"/>

    This line allows autocompletion:

    <item name="omnisharpSln" value="."/>

Creation Your Own Commands

You need to change Commander.cs - write your command in commands list:

public void Init(MainForm mainForm, Settings settings, TempSettings tempSettings)
{
    this.mainForm = mainForm;
    this.settings = settings;
    this.tempSettings = tempSettings;

    history = tempSettings.CommandHistory;
    commands.Add(new Command("help", "", "open/close tab with help text", DoHelp));
    ...
    commands.Add(new Command("replb", "[{…}]command", "open REPL bottom", DoReplBottom));
}

Explore enum command, that enters numbers at every cursor:

commands.Add(new Command("enum", "[n0] [step] [count]", "...", DoEnum));
commands.Add(new Command("enum0", "[n0] [step] [count]", "...", DoEnum0));
commands.Add(new Command("enumr", "[n0] [step] [count]", "...", DoEnumr));
...
private void DoEnum(string text)
{
    ProcessEnum(text, EnumGenerator.Mode.Number);
}

private void DoEnum0(string text)
{
    ProcessEnum(text, EnumGenerator.Mode.ZeroBeforeNumber);
}

private void DoEnumr(string text)
{
    ProcessEnum(text, EnumGenerator.Mode.Roman);
}

private void ProcessEnum(string text, EnumGenerator.Mode mode)
{
    ...
    EnumGenerator generator = new EnumGenerator
                  (text, lastBuffer.Controller.SelectionsCount, mode);
    ...
    lastBuffer.Controller.InsertTexts(generator.texts.ToArray());
}

How to Append Menu Item

Common item you can append inside MainForm.cs:

private void BuildMenu()
{
    keyMap = new KeyMap();
    doNothingKeyMap = new KeyMap();

    doNothingKeyMap.AddItem(new KeyItem(Keys.Escape, null, KeyAction.Nothing));
    doNothingKeyMap.AddItem(new KeyItem(Keys.Escape | Keys.Shift, null, KeyAction.Nothing));
    doNothingKeyMap.AddItem(new KeyItem(Keys.Control | Keys.J, null, KeyAction.Nothing));
    doNothingKeyMap.AddItem(new KeyItem(Keys.Control | Keys.K, null, KeyAction.Nothing));

    keyMap.AddItem(new KeyItem(Keys.Control | Keys.N, null,
                               new KeyAction("&File\\New", DoNew, null, false)));
    keyMap.AddItem(new KeyItem(Keys.Control | Keys.O, null,
                               new KeyAction("&File\\Open", DoOpen, null, false)));
    keyMap.AddItem(new KeyItem(Keys.None, null, new KeyAction("&File\\-",
                               null, null, false)));
    ...
    keyMap.AddItem(new KeyItem(Keys.None, null,
    new KeyAction("&?\\Kate syntax highlighting help…", DoOpenSyntaxHelp, null, false)));
}
...
private bool DoOpenSyntaxHelp(Controller controller)
{
    OpenDocument(Path.Combine(AppPath.StartupDir, "syntax/syntax.html"));
    return true;
}

True command result stops shortcut reaction by other actions with equal shortcut keys.

Autocomplete Implementation

Make simple autocomplete for example:

  • Create your own autocomplete command:
    commands.Add(new Command("simple-autocomplete", "",
                             "simple autocomplete", DoSimpleAutocomplete));
    ...
    public void DoSimpleAutocomplete(string text)
    {
        Buffer lastBuffer = mainForm.LastBuffer;
        if (lastBuffer == null)
        {
            mainForm.Dialogs.ShowInfo("Error", "No buffer");
            return;
        }
        Selection selection = lastBuffer.Controller.LastSelection;
        Place place = lastBuffer.Controller.Lines.PlaceOf(selection.anchor);
        string word = lastBuffer.Controller.GetLeftWord(place);
        List<Variant> variants = new List<Variant>();
        for (int i = 1; i <= 3; i++)
        {
            Variant variant = new Variant();
            variant.CompletionText = "simple_" + i;
            variant.DisplayText = "simple_" + i;
            variants.Add(variant);
        }
        if (mainForm.LastFrame.AsFrame != null)
            mainForm.LastFrame.AsFrame.ShowAutocomplete(variants, word);
    }
    
  • Setup shortuct for autocomplete inside text files in config:
    <item name="ctrlSpaceCommand:*.txt" value="simple-autocomplete"/>
    

History

  • 05.11.2018 First change

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here