Add plugin deinitialization, configuration saving
[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     public override void Deinit(WebThing wt) {
32         SaveConfig();
33     }
34
35     private void Window_KeyPress(object o, KeyPressEventArgs e) {
36         if ((Gdk.ModifierType.ControlMask & e.Event.State) != 0) {
37             switch(e.Event.Key) {
38             case Gdk.Key.n:
39                 wt.Tabs.NextPage();
40                 break;
41             case Gdk.Key.p:
42                 wt.Tabs.PrevPage();
43                 break;
44             }
45         } else {
46             switch(e.Event.Key) {
47             case Gdk.Key.o:
48                 CommandStart("open ");
49                 break;
50             case Gdk.Key.O:
51                 CommandStart("open " + wt.WebView.MainFrame.Uri);
52                 break;
53             case Gdk.Key.t:
54                 CommandStart("tabopen ");
55                 break;
56             case Gdk.Key.T:
57                 CommandStart("tabopen " + wt.WebView.MainFrame.Uri);
58                 break;
59             case Gdk.Key.colon:
60                 CommandlineShow();
61                 break;
62             }
63         }
64     }
65
66     private void WebView_KeyPress(object o, KeyPressEventArgs e) {
67         WebView wv = (WebView)o;
68
69         Console.WriteLine(e.Event.Key);
70         if (e.Event.State == Gdk.ModifierType.None) {
71             switch(e.Event.Key) {
72             case Gdk.Key.j:
73                 wt.ScrolledWindow.Vadjustment.Value += wt.ScrolledWindow.Vadjustment.StepIncrement;
74                 break;
75             case Gdk.Key.k:
76                 wt.ScrolledWindow.Vadjustment.Value -= wt.ScrolledWindow.Vadjustment.StepIncrement;
77                 break;
78             case Gdk.Key.l:
79                 wt.ScrolledWindow.Hadjustment.Value += wt.ScrolledWindow.Hadjustment.StepIncrement;
80                 break;
81             case Gdk.Key.h:
82                 wt.ScrolledWindow.Hadjustment.Value -= wt.ScrolledWindow.Hadjustment.StepIncrement;
83                 break;
84             case Gdk.Key.r:
85                 wv.Reload();
86                 break;
87             case Gdk.Key.Escape:
88                 wv.ExecuteScript("document.activeElement.blur()");
89                 break;
90             }
91         }
92     }
93
94     public void CommandStart(string text) {
95         commandline.Text = text;
96         commandline.GrabFocus();
97         commandline.Position = text.Length;
98         commandline.Show();
99     }
100
101     public void CommandlineShow() {
102         commandline.Show();
103         commandline.GrabFocus();
104     }
105
106     public void CommandlineHide() {
107         commandline.Hide();
108         wt.WebView.GrabFocus();
109     }
110
111     private void command_Activate(object o, EventArgs e) {
112         string[] args = Regex.Split(commandline.Text, @"\s+");
113         switch(args[0]) {
114         case "close":
115             wt.CloseTab();
116             break;
117         case "open":
118             if (args.Length < 2) return;
119             wt.OpenUri(args[1]);
120             break;
121         case "tabopen":
122             if (args.Length < 2) return;
123             wt.OpenUriTab(args[1]);
124             wt.Tabs.CurrentPage = wt.Tabs.NPages - 1;
125             break;
126         case "n":
127             wt.Tabs.NextPage();
128             break;
129         case "p":
130             wt.Tabs.PrevPage();
131             break;
132         case "q":
133             wt.Quit();
134             break;
135         case "set":
136             if (args.Length == 2)
137                 Options[args[1]] = null;
138             else
139                 Options[args[1]] = args[2];
140             ApplyOptions();
141             break;
142         }
143         CommandlineHide();
144     }
145
146     private void ApplyOptions() {
147         foreach (string key in Options.Keys) {
148             switch(key) {
149             case "ShowTabs":
150                 bool v = ParseBool(Options[key]);
151                 wt.Tabs.ShowTabs = v;
152                 break;
153             default:
154                 Error("No variable {0}", key);
155                 break;
156             }
157         }
158     }
159
160     private bool ParseBool(string v) {
161         switch(v.ToLower()) {
162         case "true":
163         case "t":
164         case "1":
165         case "on":
166         case "yes":
167             return true;
168         }
169         return false;
170     }
171
172     private void Error(string format, params object[] values) {
173         Console.WriteLine(format, values);
174     }
175
176     private void command_KeyPress(object o, KeyPressEventArgs e) {
177         if (e.Event.Key == Gdk.Key.Escape)
178             CommandlineHide();
179     }
180 }