Fix scrolling functionality
[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.Bump(0, 1);
70                 break;
71             case Gdk.Key.k:
72                 wt.Bump(0, -1);
73                 break;
74             case Gdk.Key.l:
75                 wt.Bump(1, 0);
76                 break;
77             case Gdk.Key.h:
78                 wt.Bump(-1, 0);
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         if (args.Length == 0) return;
110         string cmd = args[0];
111         string[] tmp = new string[args.Length - 1];
112         Array.Copy(args, 1, tmp, 0, tmp.Length);
113         args = tmp;
114         string query = Regex.Replace(commandline.Text, String.Format(@"^{0}\s+", cmd), "");
115
116         switch(cmd) {
117         case "close":
118             wt.CloseTab();
119             break;
120         case "open":
121             if (args.Length < 1) return;
122             if (!wt.Open(query))
123                 Error("Could not open query");
124             break;
125         case "tabopen":
126             if (args.Length < 1) return;
127             if (wt.OpenTab(query))
128                 wt.Tabs.CurrentPage = wt.Tabs.NPages - 1;
129             else
130                 Error("Could not open query");
131             break;
132         case "n":
133             wt.Tabs.NextPage();
134             break;
135         case "p":
136             wt.Tabs.PrevPage();
137             break;
138         case "q":
139             wt.Quit();
140             break;
141         case "set":
142             if (args.Length == 1)
143                 Options[args[0]] = null;
144             else
145                 Options[args[0]] = args[1];
146             ApplyOptions();
147             break;
148         case "save":
149             SaveConfig();
150             break;
151         default:
152             bool found;
153             if (args.Length > 0) {
154                 found = wt.Plugins.Call(cmd, args);
155                 if (!found)
156                     Error("No function {0}({1}) found", cmd, String.Join(", ", args));
157             } else {
158                 found = wt.Plugins.Call(cmd);
159                 if (!found)
160                     Error("No function {0}() found", cmd);
161             }
162             if (found)
163                 Console.WriteLine("Plugin function {0} called successfully", cmd);
164             break;
165         }
166         CommandlineHide();
167     }
168
169     private void ApplyOptions() {
170         foreach (string key in Options.Keys) {
171             switch(key) {
172             case "ShowTabs":
173                 bool v = Config.ParseBool(Options[key]);
174                 wt.Tabs.ShowTabs = v;
175                 break;
176             default:
177                 Error("No variable {0}", key);
178                 break;
179             }
180         }
181     }
182
183     private void Error(string format, params object[] values) {
184         Console.WriteLine(format, values);
185     }
186
187     private void command_KeyPress(object o, KeyPressEventArgs e) {
188         if (e.Event.Key == Gdk.Key.Escape)
189             CommandlineHide();
190     }
191 }