content-buffer: Make URL loading more robust to errors from invalid URLs
[conkeror.git] / content / keyboard-setup.js
blob855542294ea502fe412d27ba3042f353bf2c9b97
1 /**
2  * (C) Copyright 2008 Jeremy Maitin-Shepard
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6 **/
8 var conkeror = Components.classes['@conkeror.mozdev.org/application;1'].getService ().wrappedJSObject;
10 var last_keydown_event = null;
13 function handle_keydown(e) {
14     e.preventDefault();
15     e.stopPropagation();
17     last_keydown_event = e;
20 function handle_keypress(e) {
21     e.preventDefault();
22     e.stopPropagation();
24     // Ignore this keypress if we don't have a keycode
25     if (!last_keydown_event)
26         return;
28     // Ignore this keypress if other modifiers are down
29     if (e.altKey  || e.ctrlKey || e.metaKey)
30         return;
32     // Ignore this keypress if we don't have a charcode
33     if (!e.charCode)
34         return;
36     // Ignore space
37     if (e.charCode == " ".charCodeAt(0))
38         return;
40     var shift = e.shiftKey;
42     var keycode = last_keydown_event.keyCode;
43     var charcode = e.charCode;
45     add_mapping(keycode, shift, charcode, true);
48 var unshifted_keycode_to_charcode = [];
49 var shifted_keycode_to_charcode = [];
51 var count = 0;
53 function add_mapping(keycode, shift, charcode, single) {
54     var table = shift ? shifted_keycode_to_charcode : unshifted_keycode_to_charcode;
56     var obj = table[keycode];
57     if (obj != null)
58         obj.element.parentNode.removeChild(obj.element);
59     else
60         ++count;
61     var g = new conkeror.dom_generator(document, conkeror.XUL_NS);
62     var list = document.getElementById("mapping_list");
63     var item = g.element("listitem");
64     g.element("listcell", item, "label", keycode);
65     var keycode_name = conkeror.keycode_to_vk_name[keycode] || "";
66     g.element("listcell", item, "label", keycode_name);
67     g.element("listcell", item, "label", shift ? "true" : "false");
68     g.element("listcell", item, "label", charcode);
69     g.element("listcell", item, "label", String.fromCharCode(charcode));
70     list.appendChild(item);
71     table[keycode] = {charcode: charcode, element: item};
72     if (single) {
73         list.ensureElementIsVisible(item);
74         message("Added mapping: "  + keycode + ((keycode_name.length > 0)? " (" + keycode_name + ")" : "") +
75                 " -> " + charcode + " ('" + String.fromCharCode(charcode) + "')");
76         update_count();
77     }
80 function update_count() {
81     document.getElementById("count").value = "Count: " + count;
84 function clear() {
85     count = 0;
86     var tables = [unshifted_keycode_to_charcode, shifted_keycode_to_charcode];
87     for each (let table in tables) {
88         for each (let x in table) {
89             x.element.parentNode.removeChild(x.element);
90         }
91     }
92     unshifted_keycode_to_charcode = [];
93     shifted_keycode_to_charcode = [];
94     update_count();
95     message("Cleared mappings.");
98 function load_tables(tables) {
99     let [unshifted_table, shifted_table] = tables;
100     clear();
101     for (let x in unshifted_table) {
102         let y = unshifted_table[x];
103         if (y != null)
104             add_mapping(x, false, y);
105     }
106     for (let x in shifted_table) {
107         let y = shifted_table[x];
108         if (y != null)
109             add_mapping(x, true, y);
110     }
111     update_count();
114 function load(use_defaults) {
115     let tables = conkeror.get_charcode_mapping_table_from_preferences();
116     if (tables != null) {
117         load_tables(tables);
118         message("Loaded mappings.");
119     } else {
120         if (use_defaults) {
121             reset();
122             message("No mapping table stored in preferences.  The default mapping table was loaded.");
123         }
124         else
125             message("Error: No mapping table stored in preferences.");
126     }
129 function save() {
130     var unshifted_table = unshifted_keycode_to_charcode.map(function (x) x.charcode);
131     var shifted_table = shifted_keycode_to_charcode.map(function (x) x.charcode);
133     var str = [unshifted_table, shifted_table].toSource();
134     conkeror.user_pref("conkeror.charCodeMappingTable", str);
135     message("Saved mappings.  Restart Conkeror for the new mapping table to take effect.");
138 function reset() {
139     let tables = conkeror.get_default_keycode_to_charcode_tables();
140     load_tables(tables);
141     message("Loaded default mapping table.");
144 function clear_preference() {
145     try {
146         conkeror.clear_pref("conkeror.charCodeMappingTable");
147         message("Cleared preference.  Restart Conkeror for the default table to take effect.");
148     } catch (e) {
149         message("Preference was already not set.");
150     }
153 function message(msg) {
154     document.getElementById("message").value = msg;
157 function init() {
158     window.addEventListener("keydown", handle_keydown, true);
159     window.addEventListener("keypress", handle_keypress, true);
161     load(true);