2 * (C) Copyright 2004-2007 Shawn Betts
3 * (C) Copyright 2007-2010 John J. Foerch
4 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
6 * Use, modification, and distribution are subject to the terms specified in the
13 * Given a callback func and an interactive context I, call func, passing
14 * either a focused field, or the minibuffer's input element if the
15 * minibuffer is active. Afterward, call `scroll_selection_into_view' on
16 * the field. See `paste_x_primary_selection' and `open_line' for
19 function call_on_focused_field (I, func, clear_mark) {
20 var m = I.window.minibuffer;
21 var s = m.current_state;
24 if (m._input_mode_enabled) {
25 m._restore_normal_state();
27 } else if ((e = I.buffer.focused_element)) {
28 if (e.getAttribute("contenteditable") == 'true')
30 } else if ((frame = I.buffer.focused_frame) &&
31 frame.document.designMode &&
32 frame.document.designMode == "on") {
37 if (m._input_mode_enabled)
38 s.mark_active = false;
40 I.buffer.mark_active = false;
42 scroll_selection_into_view(e);
47 * Replace the current region with modifier(selection). Deactivates region
48 * and sets point to the end of the inserted text. The modifier can
49 * control the exact placement of point by returning an array
50 * [replacement, point_offset] instead of just a string.
52 function modify_region (field, modifier) {
53 if (field instanceof Ci.nsIDOMDocument) {
55 var win = field.defaultView;
56 var sel = win.getSelection();
57 let replacement = modifier(win.getSelection().toString());
59 if (array_p(replacement)) {
60 point = replacement[1];
61 replacement = replacement[0];
63 point = replacement.length;
64 field.execCommand("insertHTML", false,
65 html_escape(replacement)
66 .replace(/\n/g, '<br>')
67 .replace(/ /g, ' '));
68 if (point != replacement.length) {
69 sel.extend(sel.focusNode, point);
70 sel.collapse(sel.focusNode, point);
75 modifier(field.value.substring(field.selectionStart, field.selectionEnd));
76 let point = field.selectionStart;
77 if (array_p(replacement)) {
78 point += replacement[1];
79 replacement = replacement[0];
81 point += replacement.length;
83 field.value.substr(0, field.selectionStart) + replacement +
84 field.value.substr(field.selectionEnd);
85 field.setSelectionRange(point, point);
91 * Takes an interactive context and a function to call with the word
92 * at point as its sole argument, and which returns a modified word.
94 //XXX: this should be implemented in terms of modify_region,
95 // in order to work in richedit fields.
96 function modify_word_at_point (field, func) {
97 // Skip any whitespaces at point and move point to the right place.
98 var point = field.selectionStart;
99 var rest = field.value.substring(point);
101 // Skip any whitespaces.
102 for (var i = 0, rlen = rest.length; i < rlen; i++) {
103 if (" \n".indexOf(rest.charAt(i)) == -1) {
109 // Find the next whitespace, as it is the end of the word. If no next
110 // whitespace is found, we're at the end of input. TODO: Add "\n" support.
111 var goal = field.value.indexOf(" ", point);
113 goal = field.value.length;
115 // Change the value of the text field.
116 var input = field.value;
118 input.substring(0, point) +
119 func(input.substring(point, goal)) +
120 input.substring(goal);
123 field.selectionStart = goal;
124 field.selectionEnd = goal;