2 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
3 * (C) Copyright 2008 John J. Foerch
5 * Use, modification, and distribution are subject to the terms specified in the
9 require("content-buffer.js");
11 define_current_buffer_hook("current_buffer_input_mode_change_hook", "input_mode_change_hook");
13 var content_buffer_input_mode_keymaps = {};
15 function define_input_mode(base_name, display_name, keymap_name, doc) {
16 var name = base_name + "_input_mode";
17 content_buffer_input_mode_keymaps[name] = keymap_name;
18 define_buffer_mode(name,
20 $class = "input_mode",
21 $enable = function (buffer) { check_buffer(buffer, content_buffer);
22 content_buffer_update_keymap_for_input_mode(buffer); },
26 ignore_function_for_get_caller_source_code_reference("define_input_mode");
28 function content_buffer_update_keymap_for_input_mode(buffer) {
29 if (buffer.input_mode)
30 buffer.keymap = buffer.get(content_buffer_input_mode_keymaps[buffer.input_mode]);
33 add_hook("page_mode_change_hook", content_buffer_update_keymap_for_input_mode);
35 add_hook("content_buffer_location_change_hook", function (buf) { normal_input_mode(buf, true); });
37 define_input_mode("normal", null, "content_buffer_normal_keymap");
39 // For SELECT elements
40 define_input_mode("select", "input:SELECT", "content_buffer_select_keymap");
42 // For text INPUT and TEXTAREA elements
43 define_input_mode("text", "input:TEXT", "content_buffer_text_keymap");
44 define_input_mode("textarea", "input:TEXTAREA", "content_buffer_textarea_keymap");
45 define_input_mode("richedit", "input:RICHEDIT", "content_buffer_richedit_keymap");
47 define_input_mode("checkbox", "input:CHECKBOX/RADIOBUTTON", "content_buffer_checkbox_keymap");
50 "quote_next", "input:PASS-THROUGH(next)", "content_buffer_quote_next_keymap",
51 "This input mode sends the next key combo to the buffer, "+
52 "bypassing Conkeror's normal key handling. The mode disengages "+
53 "after one key combo.");
55 "quote", "input:PASS-THROUGH", "content_buffer_quote_keymap",
56 "This input mode sends all key combos to the buffer, "+
57 "bypassing Conkeror's normal key handling, until the "+
58 "Escape key is pressed.");
60 define_input_mode("caret", null, "content_buffer_caret_keymap");
62 function content_buffer_update_input_mode_for_focus(buffer, force) {
63 var mode = buffer.input_mode;
64 var form_input_mode_enabled = (mode == "text_input_mode" ||
65 mode == "textarea_input_mode" ||
66 mode == "select_input_mode" ||
67 mode == "checkbox_input_mode" ||
68 mode == "richedit_input_mode");
70 if (force || form_input_mode_enabled || mode == "normal_input_mode") {
71 let elem = buffer.focused_element;
74 var input_mode_function = null;
75 if (elem instanceof Ci.nsIDOMHTMLInputElement) {
76 var type = elem.getAttribute("type");
77 if (type != null) type = type.toLowerCase();
78 if (type == "checkbox" || type == "radio")
79 input_mode_function = checkbox_input_mode;
80 else if (type != "submit" &&
82 input_mode_function = text_input_mode;
84 else if (elem instanceof Ci.nsIDOMHTMLTextAreaElement)
85 input_mode_function = textarea_input_mode;
87 else if (elem instanceof Ci.nsIDOMHTMLSelectElement)
88 input_mode_function = select_input_mode;
91 if (!input_mode_function) {
93 let frame = buffer.focused_frame;
94 let in_design_mode = false;
95 if (frame && frame.document.designMode == "on")
96 in_design_mode = true;
100 switch (elem.contentEditable) {
102 in_design_mode = true;
106 default: // == "inherit"
107 elem = elem.parentNode;
112 input_mode_function = richedit_input_mode;
115 if (input_mode_function) {
117 browser_prevent_automatic_form_focus_mode_enabled &&
118 !form_input_mode_enabled &&
119 (buffer.last_user_input_received == null ||
120 (Date.now() - buffer.last_user_input_received)
121 > browser_automatic_form_focus_window_duration)) {
122 // Automatic focus attempt blocked
125 input_mode_function(buffer, true);
129 normal_input_mode(buffer, true);
133 add_hook("content_buffer_focus_change_hook", function (buf) { content_buffer_update_input_mode_for_focus(buf, false); } );
135 define_buffer_mode('caret_mode', 'CARET',
136 $enable = function(buffer) {
137 buffer.browser.setAttribute('showcaret', 'true');
138 let sc = getFocusedSelCtrl(buffer);
139 sc.setCaretEnabled(true);
140 buffer.top_frame.focus();
141 caret_input_mode(buffer, true);
143 $disable = function(buffer) {
144 buffer.browser.setAttribute('showcaret', '');
145 let sc = getFocusedSelCtrl(buffer);
146 sc.setCaretEnabled(false);
147 buffer.browser.focus();
148 content_buffer_update_input_mode_for_focus(buffer, true);
151 watch_pref(CARET_PREF, function() {
152 if (get_pref(CARET_PREF)) {
153 session_pref(CARET_PREF, false);
154 let window = window_watcher.activeWindow;
155 let buffer = window.buffers.current;
160 interactive("content-buffer-update-input-mode-for-focus", null, function (I) {
161 content_buffer_update_input_mode_for_focus(I.buffer, true);
164 function minibuffer_input_mode_indicator(window) {
165 this.window = window;
166 this.hook_func = method_caller(this, this.update);
167 add_hook.call(window, "select_buffer_hook", this.hook_func);
168 add_hook.call(window, "current_buffer_input_mode_change_hook", this.hook_func);
172 minibuffer_input_mode_indicator.prototype = {
173 update : function () {
174 var buf = this.window.buffers.current;
175 var mode = buf.input_mode;
177 this.window.minibuffer.element.className = "minibuffer-" + buf.input_mode.replace("_","-","g");
179 uninstall : function () {
180 remove_hook.call(window, "select_buffer_hook", this.hook_func);
181 remove_hook.call(window, "current_buffer_input_mode_change_hook", this.hook_func);
185 define_global_window_mode("minibuffer_input_mode_indicator", "window_initialize_hook");
186 minibuffer_input_mode_indicator_mode(true);
189 define_variable("browser_automatic_form_focus_window_duration", 20, "Time window (in milliseconds) during which a form element is allowed to gain focus following a mouse click or key press, if `browser_prevent_automatic_form_focus_mode' is enabled.");;
191 define_global_mode("browser_prevent_automatic_form_focus_mode",
192 function () {}, // enable
193 function () {} // disable
195 browser_prevent_automatic_form_focus_mode(true);
198 "browser_form_field_xpath_expression",
200 // "translate(@type,'RADIO','radio')!='radio' and " +
201 // "translate(@type,'CHECKBOX','checkbox')!='checkbox' and " +
202 "translate(@type,'HIDEN','hiden')!='hidden'"
203 // "translate(@type,'SUBMIT','submit')!='submit' and " +
204 // "translate(@type,'REST','rest')!='reset'"
207 // "translate(@type,'RADIO','radio')!='radio' and " +
208 // "translate(@type,'CHECKBOX','checkbox')!='checkbox' and " +
209 "translate(@type,'HIDEN','hiden')!='hidden'"
210 // "translate(@type,'SUBMIT','submit')!='submit' and " +
211 // "translate(@type,'REST','rest')!='reset'"
213 "//select | //xhtml:select | " +
214 "//textarea | //xhtml:textarea | " +
215 "//textbox | //xul:textbox",
216 "XPath expression matching elements to be selected by `browser-focus-next-form-field' " +
217 "and `browser-focus-previous-form-field.'");
219 function browser_focus_next_form_field(buffer, count, xpath_expr) {
220 var focused_elem = buffer.focused_element;
222 return; // invalid count
224 function helper(win, skip_win) {
227 var doc = win.document;
228 var res = doc.evaluate(xpath_expr, doc, xpath_lookup_namespace,
229 Ci.nsIDOMXPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
230 null /* existing results */);
231 var length = res.snapshotLength;
234 if (focused_elem != null) {
235 for (var i = 0; i < length; ++i) {
236 if (res.snapshotItem(i) == focused_elem) {
249 index = index + count;
250 index = index % length;
254 return res.snapshotItem(index);
257 // Recurse on sub-frames
258 for (var i = 0; i < win.frames.length; ++i) {
259 var elem = helper(win.frames[i], skip_win);
266 var focused_win = buffer.focused_frame;
267 var elem = helper(focused_win, null);
269 elem = helper(buffer.top_frame, focused_win);
271 browser_element_focus(buffer, elem);
273 throw interactive_error("No form field found");
276 interactive("browser-focus-next-form-field", "Focus the next element matching `browser_form_field_xpath_expression'.",
278 browser_focus_next_form_field(I.buffer, I.p, browser_form_field_xpath_expression);
281 interactive("browser-focus-previous-form-field", "Focus the previous element matching `browser_form_field_xpath_expression'.",
283 browser_focus_next_form_field(I.buffer, -I.p, browser_form_field_xpath_expression);
286 function edit_field_in_external_editor(buffer, elem) {
287 if (elem instanceof Ci.nsIDOMHTMLInputElement) {
288 var type = elem.getAttribute("type");
290 type = type.toLowerCase();
291 if (type == "hidden" || type == "checkbox" || type == "radio")
292 throw interactive_error("Element is not a text field.");
293 } else if (!(elem instanceof Ci.nsIDOMHTMLTextAreaElement))
294 throw interactive_error("Element is not a text field.");
296 var name = elem.getAttribute("name");
297 if (!name || name.length == 0)
298 name = elem.getAttribute("id");
301 name = name.replace(/[^a-zA-Z0-9\-_]/g, "");
302 if (name.length == 0)
305 var file = get_temporary_file(name);
309 write_text_file(file, elem.value);
315 // FIXME: decide if we should do this
316 var old_class = elem.className;
317 elem.className = "__conkeror_textbox_edited_externally " + old_class;
320 yield open_file_with_external_editor(file);
322 elem.value = read_text_file(file);
324 elem.className = old_class;
329 interactive("edit-current-field-in-external-editor", "Edit the contents of the currently-focused text field in an external editor.",
332 yield edit_field_in_external_editor(buf, buf.focused_element);
333 unfocus(I.window, buf);
336 define_variable("kill_whole_line", false, "If true, `kill-line' with no arg at beg of line kills the whole line.");
338 function cut_to_end_of_line (buffer) {
339 var elem = buffer.focused_element;
341 var st = elem.selectionStart;
342 var en = elem.selectionEnd;
344 // there is no selection. set one up.
345 var eol = elem.value.indexOf ("\n", en);
348 elem.selectionEnd = elem.textLength;
349 } else if (eol == st) {
350 elem.selectionEnd = eol + 1;
351 } else if (kill_whole_line &&
352 (st == 0 || elem.value[st - 1] == "\n"))
354 elem.selectionEnd = eol + 1;
356 elem.selectionEnd = eol;
359 buffer.do_command ('cmd_cut');
361 /* FIXME: Make this work for richedit mode as well */
366 "cut-to-end-of-line",
369 cut_to_end_of_line (I.buffer);