special-buffer.js: Change to use a chrome:// version of about:blank to work around...
[conkeror.git] / content / keyboard-setup.js
blob4fc4803a1d2de51adadfc40e0cf9806cf7f05b08
1 var conkeror = Components.classes['@conkeror.mozdev.org/application;1'].getService ().wrappedJSObject;
3 var last_keydown_event = null;
6 function handle_keydown(e) {
7     e.preventDefault();
8     e.stopPropagation();
10     last_keydown_event = e;
13 function handle_keypress(e) {
14     e.preventDefault();
15     e.stopPropagation();
17     // Ignore this keypress if we don't have a keycode
18     if (!last_keydown_event)
19         return;
21     // Ignore this keypress if other modifiers are down
22     if (e.altKey  || e.ctrlKey || e.metaKey)
23         return;
25     // Ignore this keypress if we don't have a charcode
26     if (!e.charCode)
27         return;
28     
29     // Ignore space
30     if (e.charCode == " ".charCodeAt(0))
31         return;
33     var shift = e.shiftKey;
35     var keycode = last_keydown_event.keyCode;
36     var charcode = e.charCode;
38     add_mapping(keycode, shift, charcode, true);
41 var unshifted_keycode_to_charcode = [];
42 var shifted_keycode_to_charcode = [];
44 var count = 0;
46 function add_mapping(keycode, shift, charcode, single) {
47     var table = shift ? shifted_keycode_to_charcode : unshifted_keycode_to_charcode;
49     var obj = table[keycode];
50     if (obj != null)
51         obj.element.parentNode.removeChild(obj.element);
52     else
53         ++count;
54     var g = new conkeror.dom_generator(document, conkeror.XUL_NS);
55     var list = document.getElementById("mapping_list");
56     var item = g.element("listitem");
57     g.element("listcell", item, "label", keycode);
58     var keycode_name = conkeror.keycode_to_vk_name[keycode] || "";
59     g.element("listcell", item, "label", keycode_name);
60     g.element("listcell", item, "label", shift ? "true" : "false");
61     g.element("listcell", item, "label", charcode);
62     g.element("listcell", item, "label", String.fromCharCode(charcode));
63     list.appendChild(item);
64     table[keycode] = {charcode: charcode, element: item};
65     if (single) {
66         list.ensureElementIsVisible(item);
67         message("Added mapping: "  + keycode + ((keycode_name.length > 0)? " (" + keycode_name + ")" : "") +
68                 " -> " + charcode + " ('" + String.fromCharCode(charcode) + "')");
69         update_count();
70     }
73 function update_count() {
74     document.getElementById("count").value = "Count: " + count;
77 function clear() {
78     count = 0;
79     var tables = [unshifted_keycode_to_charcode, shifted_keycode_to_charcode];
80     for each (let table in tables) {
81         for each (let x in table) {
82             x.element.parentNode.removeChild(x.element);
83         }
84     }
85     unshifted_keycode_to_charcode = [];
86     shifted_keycode_to_charcode = [];
87     update_count();
88     message("Cleared mappings.");
91 function load_tables(tables) {
92     let [unshifted_table, shifted_table] = tables;
93     clear();
94     for (let x in unshifted_table) {
95         let y = unshifted_table[x];
96         if (y != null)
97             add_mapping(x, false, y);
98     }
99     for (let x in shifted_table) {
100         let y = shifted_table[x];
101         if (y != null)
102             add_mapping(x, true, y);
103     }
104     update_count();
107 function load(use_defaults) {
108     let tables = conkeror.get_charcode_mapping_table_from_preferences();
109     if (tables != null) {
110         load_tables(tables);
111         message("Loaded mappings.");
112     } else {
113         if (use_defaults) {
114             reset();
115             message("No mapping table stored in preferences.  The default mapping table was loaded.");
116         }
117         else
118             message("Error: No mapping table stored in preferences.");
119     }
122 function save() {
123     var unshifted_table = unshifted_keycode_to_charcode.map(function (x) x.charcode);
124     var shifted_table = shifted_keycode_to_charcode.map(function (x) x.charcode);
126     var str = [unshifted_table, shifted_table].toSource();
127     conkeror.user_pref("conkeror.charCodeMappingTable", str);
128     message("Saved mappings.  Restart Conkeror for the new mapping table to take effect.");
131 function reset() {
132     let tables = conkeror.get_default_keycode_to_charcode_tables();
133     load_tables(tables);
134     message("Loaded default mapping table.");
137 function clear_preference() {
138     try {
139         conkeror.clear_pref("conkeror.charCodeMappingTable");
140         message("Cleared preference.  Restart Conkeror for the default table to take effect.");
141     } catch (e) {
142         message("Preference was already not set.");
143     }
146 function message(msg) {
147     document.getElementById("message").value = msg;
150 function init() {
151     window.addEventListener("keydown", handle_keydown, true);
152     window.addEventListener("keypress", handle_keypress, true);
154     load(true);