Example #1
0
File: Main.cs Project: ktvoelker/di
        public Main(Model.Main m)
        {
            Model = m;
            Idle = new IdleHandler(this);

            WindowModes = new Dictionary<string, IEnumerable<WindowMode>>();

            // Load command macros from config file
            foreach (var entry in Model.Config.GetSectionOrEmpty("macros"))
            {
                commandMacros[entry.Key] = ParseCommands(entry.Value);
            }

            // Load window modes from config file
            foreach (var section in Model.Config)
            {
                var match = ModeSectionName.Match(section.Key);
                if (match.Success)
                {
                    var modeKey = match.Groups["name"].Value;
                    var mode = new WindowMode();
                    mode.Key = modeKey;
                    mode.Name = section.Value.GetWithDefault<string, string>("display-name", modeKey);
                    mode.Hidden = section.Value.GetBoolWithDefault("hidden", false);
                    var map = new KeyMap();
                    sbyte prio = 0;
                    if (section.Value.ContainsKey("priority"))
                    {
                        prio = sbyte.Parse(section.Value["priority"]);
                        mode.Priority = prio;
                    }
                    if (section.Value.ContainsKey("default"))
                    {
                        map.SetDefault(prio, ParseCommands(section.Value["default"]));
                    }
                    foreach (var entry in section.Value)
                    {
                        if (string.IsNullOrEmpty(entry.Key) || entry.Key == "display-name" || entry.Key == "hidden" || entry.Key == "default")
                        {
                            continue;
                        }
                        var mod = Gdk.ModifierType.None;
                        var tokens = entry.Key.Tokenize().ToList();
                        var key = (Gdk.Key) (Gdk.Keyval.FromName(tokens.Last()));
                        tokens.RemoveAt(tokens.Count - 1);
                        foreach (var modName in tokens)
                        {
                            mod |= ModifierNames[modName];
                        }
                        map.Add(key, mod, prio, ParseCommands(entry.Value));
                    }
                    mode.KeyMap = map;
                    WindowModes.Add(modeKey, new WindowMode[] { mode });
                }
            }

            // Load mode sets from config file
            foreach (var entry in Model.Config.GetSectionOrEmpty("mode-sets"))
            {
                WindowModes[entry.Key] = entry.Value.Tokenize().Select(k => WindowModes[k]).Flatten();
            }

            // Load default window modes from config file
            Model.Config[""]["default-modes"].Tokenize().ForEach(k => WindowModes[k].ForEach(Window.DefaultMode.Add));

            Windows = new BindListWithCurrent<Window>();
            if (Model.Buffers.HasAny())
            {
                Windows.Add(new Window(this, Model.Buffers.Item(0)));
            }

            Action openFile = () =>
            {
                if (Windows.Count == 0)
                {
                    Command.FileCommand.OpenFile(this, Command.FileCommand.InNewWindow, false);
                }
            };

            // Open a file at startup (but it won't work until the view has attached itself to our events)
            Ready.Add(openFile);

            // Open a file whenever all other files have been closed
            Windows.Changed.Add(openFile);
        }
Example #2
0
File: Main.cs Project: cpdean/di
        public Main(Model.Main m)
        {
            Model = m;

            var windowModes = new List<WindowMode>();
            WindowModes = new ReadOnlyCollection<WindowMode>(windowModes);

            // Command mode bindings (0)
            var commandMode = new KeyMap() { Priority = 5 };
            commandMode.Add(Key.i, new Command.ClearWindowMode(), new Command.AddWindowMode(1));
            commandMode.Add(Key.h, new Command.Down());
            commandMode.Add(Key.t, new Command.Up());
            commandMode.Add(Key.d, new Command.Left());
            commandMode.Add(Key.n, new Command.Right());
            commandMode.Add(Key.Down, new Command.Down());
            commandMode.Add(Key.Up, new Command.Up());
            commandMode.Add(Key.Left, new Command.Left());
            commandMode.Add(Key.Right, new Command.Right());
            commandMode.Add(Key.o, new Command.OpenFile());
            commandMode.Add(Key.w, new Command.ClearWindowMode(), new Command.AddWindowMode(4), new Command.AddWindowMode(3));
            windowModes.Add(new WindowMode { Name = "Command", KeyMap = commandMode });

            // Insert mode bindings (1)
            var insertMode = new KeyMap();
            insertMode.SetDefault(new Command.InsertKey());
            insertMode.Add(Key.Return, new Command.InsertChar('\n'));
            insertMode.Add(Key.Escape,
                           new Command.DiscardInput(),
                           new Command.ClearWindowMode(),
                           new Command.AddWindowMode(0),
                           new Command.AddWindowMode(2),
                           new Command.AddWindowMode(3));
            insertMode.Add(Key.BackSpace, new Command.Delete(), new Command.Backspace());
            insertMode.Add(Key.Tab, new Command.Tab());
            insertMode.Add(Key.Down, new Command.Down());
            insertMode.Add(Key.Up, new Command.Up());
            insertMode.Add(Key.Left, new Command.Left());
            insertMode.Add(Key.Right, new Command.Right());
            insertMode.Add(Key.Delete, new Command.Delete(), new Command.Right());
            windowModes.Add(new WindowMode { Name = "Insert", KeyMap = insertMode });

            // Number mode bindings (2)
            var numberMode = new KeyMap();
            numberMode.Add(Key.Key_0, new NumCommand());
            numberMode.Add(Key.Key_1, new NumCommand());
            numberMode.Add(Key.Key_2, new NumCommand());
            numberMode.Add(Key.Key_3, new NumCommand());
            numberMode.Add(Key.Key_4, new NumCommand());
            numberMode.Add(Key.Key_5, new NumCommand());
            numberMode.Add(Key.Key_6, new NumCommand());
            numberMode.Add(Key.Key_7, new NumCommand());
            numberMode.Add(Key.Key_8, new NumCommand());
            numberMode.Add(Key.Key_9, new NumCommand());
            windowModes.Add(new WindowMode { Name = "Number", Hidden = true, KeyMap = numberMode });

            // Common bindings (3)
            var commonMode = new KeyMap();
            commonMode.Add(Key.Escape, new Command.DiscardInput());
            windowModes.Add(new WindowMode { Name = "Common", Hidden = true, KeyMap = commonMode });

            // Window mode bindings (4)
            var windowMode = new KeyMap();
            windowMode.Add(Key.a,
                           new Command.OpenFileInNewWindow(),
                           new Command.ClearWindowMode(),
                           new Command.AddWindowMode(0),
                           new Command.AddWindowMode(2),
                           new Command.AddWindowMode(3));
            windowMode.Add(Key.c,
                           new Command.CloseWindow(),
                           new Command.ClearWindowMode(),
                           new Command.AddWindowMode(0),
                           new Command.AddWindowMode(2),
                           new Command.AddWindowMode(3));
            windowModes.Add(new WindowMode { Name = "Window", KeyMap = windowMode });

            Windows = new BindList<Window>();
            if (Model.Buffers.HasAny())
            {
                var window = new Window(this, Model.Buffers.Item(0));
                Windows.Add(window);
                FocusedWindow.Value = window;
            }
            WindowsEvents = Windows.Event;
        }