Fixed Session so that CurrentSession is set in RestoreSession
[WebThing.git] / plugins / Vimish.cs
1 using System;
2 using System.Text.RegularExpressions;
3 using Gtk;
4 using WebKit;
5 using bytex64.WebThing;
6
7 public class Vimish : WebThingPlugin {
8     WebThing wt;
9     Gtk.Entry commandline;
10
11     public override void Init(WebThing wt) {
12         this.wt = wt;
13         wt.Tabs.ShowTabs = false;
14
15         wt.Window.KeyPressEvent += Window_KeyPress;
16
17         commandline = new Gtk.Entry();
18         commandline.Activated += command_Activate;
19         commandline.KeyPressEvent += command_KeyPress;
20         wt.AttachWidget(commandline, AttachPoint.S, AttachOptions.Fill, AttachOptions.Shrink);
21
22         commandline.Hide();
23
24         ApplyOptions();
25     }
26
27     public override void InitWebView(WebView wv) {
28         wv.KeyPressEvent += WebView_KeyPress;
29     }
30
31     private void Window_KeyPress(object o, KeyPressEventArgs e) {
32         if ((Gdk.ModifierType.ControlMask & e.Event.State) != 0) {
33             switch(e.Event.Key) {
34             case Gdk.Key.n:
35                 wt.Tabs.NextPage();
36                 break;
37             case Gdk.Key.p:
38                 wt.Tabs.PrevPage();
39                 break;
40             }
41         } else {
42             switch(e.Event.Key) {
43             case Gdk.Key.o:
44                 CommandStart("open ");
45                 break;
46             case Gdk.Key.O:
47                 CommandStart("open " + wt.WebView.MainFrame.Uri);
48                 break;
49             case Gdk.Key.t:
50                 CommandStart("tabopen ");
51                 break;
52             case Gdk.Key.T:
53                 CommandStart("tabopen " + wt.WebView.MainFrame.Uri);
54                 break;
55             case Gdk.Key.colon:
56                 CommandlineShow();
57                 break;
58             }
59         }
60     }
61
62     private void WebView_KeyPress(object o, KeyPressEventArgs e) {
63         WebView wv = (WebView)o;
64
65         Console.WriteLine(e.Event.Key);
66         if (e.Event.State == Gdk.ModifierType.None) {
67             switch(e.Event.Key) {
68             case Gdk.Key.j:
69                 wt.ScrolledWindow.Vadjustment.Value += wt.ScrolledWindow.Vadjustment.StepIncrement;
70                 break;
71             case Gdk.Key.k:
72                 wt.ScrolledWindow.Vadjustment.Value -= wt.ScrolledWindow.Vadjustment.StepIncrement;
73                 break;
74             case Gdk.Key.l:
75                 wt.ScrolledWindow.Hadjustment.Value += wt.ScrolledWindow.Hadjustment.StepIncrement;
76                 break;
77             case Gdk.Key.h:
78                 wt.ScrolledWindow.Hadjustment.Value -= wt.ScrolledWindow.Hadjustment.StepIncrement;
79                 break;
80             case Gdk.Key.r:
81                 wv.Reload();
82                 break;
83             case Gdk.Key.Escape:
84                 wv.ExecuteScript("document.activeElement.blur()");
85                 break;
86             }
87         }
88     }
89
90     public void CommandStart(string text) {
91         commandline.Text = text;
92         commandline.GrabFocus();
93         commandline.Position = text.Length;
94         commandline.Show();
95     }
96
97     public void CommandlineShow() {
98         commandline.Show();
99         commandline.GrabFocus();
100     }
101
102     public void CommandlineHide() {
103         commandline.Hide();
104         wt.WebView.GrabFocus();
105     }
106
107     private void command_Activate(object o, EventArgs e) {
108         string[] args = Regex.Split(commandline.Text, @"\s+");
109         switch(args[0]) {
110         case "close":
111             wt.CloseTab();
112             break;
113         case "open":
114             if (args.Length < 2) return;
115             wt.OpenUri(args[1]);
116             break;
117         case "tabopen":
118             if (args.Length < 2) return;
119             wt.OpenUriTab(args[1]);
120             wt.Tabs.CurrentPage = wt.Tabs.NPages - 1;
121             break;
122         case "n":
123             wt.Tabs.NextPage();
124             break;
125         case "p":
126             wt.Tabs.PrevPage();
127             break;
128         case "q":
129             wt.Quit();
130             break;
131         case "set":
132             if (args.Length == 2)
133                 Options[args[1]] = null;
134             else
135                 Options[args[1]] = args[2];
136             ApplyOptions();
137             break;
138         case "save":
139             SaveConfig();
140             break;
141         default:
142             bool found;
143             if (args.Length > 1) {
144                 string[] callargs = new string[args.Length - 1];
145                 Array.Copy(args, 1, callargs, 0, args.Length - 1);
146                 found = wt.Plugins.Call(args[0], callargs);
147                 if (!found)
148                     Error("No function {0}({1}) found", args[0], String.Join(", ", callargs));
149             } else {
150                 found = wt.Plugins.Call(args[0]);
151                 if (!found)
152                     Error("No function {0}() found", args[0]);
153             }
154             if (found)
155                 Console.WriteLine("Plugin function {0} called successfully", args[0]);
156             break;
157         }
158         CommandlineHide();
159     }
160
161     private void ApplyOptions() {
162         foreach (string key in Options.Keys) {
163             switch(key) {
164             case "ShowTabs":
165                 bool v = Config.ParseBool(Options[key]);
166                 wt.Tabs.ShowTabs = v;
167                 break;
168             default:
169                 Error("No variable {0}", key);
170                 break;
171             }
172         }
173     }
174
175     private void Error(string format, params object[] values) {
176         Console.WriteLine(format, values);
177     }
178
179     private void command_KeyPress(object o, KeyPressEventArgs e) {
180         if (e.Event.Key == Gdk.Key.Escape)
181             CommandlineHide();
182     }
183 }